Python 如何在 django admin 上添加只读内联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17118320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:30:54  来源:igfitidea点击:

How to add readonly inline on django admin

pythondjangoadmin

提问by Hassek

I am using django 1.4 and I have a many2many field, so when creating the admin site I wanted to add this field as an inline, here is some code:

我正在使用 django 1.4 并且我有一个 many2many 字段,所以在创建管理站点时我想将此字段添加为内联,这是一些代码:

class SummaryInline(admin.TabularInline):
    model = ParserError.summaries.through


class MyClassAdmin(admin.ModelAdmin):
    list_display = ('classifier', 'name', 'err_count', 'supported')
    fields = ('classifier', 'name', 'err_count', 'err_classifier', 'supported')
    inlines = (SummaryInline,)
    readonly_fields = ('classifier', 'err_count')

So my question is, how can I make the inline field readonly?

所以我的问题是,如何使内联字段只读?

采纳答案by Hassek

After a while of trying to find the name I figured out thanks to this answer, so I checked the names at self.opts.local_fieldsand found the name of the middle table and added it to readonly_fields, setting can_deleteto False.

多亏了这个答案,我尝试了一段时间后找到了名称,所以我检查了名称,self.opts.local_fields找到了中间表的名称并将其添加到readonly_fields,设置can_delete为 False。

class SummaryInline(admin.TabularInline):
    model = ParserError.summaries.through
    readonly_fields = ('myclasssummary',)
    can_delete = False

pretty simple but took me a while so I figured out it was a good idea to add it here.

很简单,但花了我一段时间,所以我想在这里添加它是个好主意。

回答by Keval Prabhu

Additionally, if you do not want the ability to add/delete the rows, you can add these definitions.

此外,如果您不想添加/删除行,您可以添加这些定义。

def has_add_permission(self, request, obj=None):
    return False

def has_delete_permission(self, request, obj=None):
    return False

回答by Muhammad Hafid

Thanks Keval Prabhu

感谢凯瓦尔·帕布

class UnitsInline(admin.TabularInline):
    model = Units
    extra = 0
    verbose_name = 'Units'
    verbose_name_plural = 'Units of company'

    **def has_add_permission(self, request, obj=None):
        return False
    def has_delete_permission(self, request, obj=None):
        return False**

回答by tamarabyte

You can make the entire inline readonly by adding:

您可以通过添加以下内容使整个内联只读:

class UnitsInline(admin.TabularInline):

    def has_change_permission(self, request, obj=None):
        return False

This will prevent anyone from editing the entry from the admin.

这将阻止任何人编辑来自管理员的条目。

Another example that prevents, adding, deletion and displays all the inline fields as readonly:

另一个防止、添加、删除和将所有内联字段显示为只读的示例:

class ReadOnlyInline(admin.TabularInline):
    def has_change_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_readonly_fields(self, request, obj=None):
        return list(super().get_fields(request, obj))