Django 管理员:仅在更改表单上排除字段

2022-01-25 00:00:00 python django django-admin

问题描述

是否有办法检测模型中的信息是否正在添加或更改.

If there a way to detect if information in a model is being added or changed.

如果有这个信息可以用来排除字段.

If there is can this information be used to exclude fields.

一些伪代码来说明我在说什么.

Some pseudocode to illustrate what I'm talking about.

class SubSectionAdmin(admin.ModelAdmin):
    if something.change_or_add = 'change':
        exclude = ('field',)
    ...

谢谢


解决方案

class SubSectionAdmin(admin.ModelAdmin):
    # ...
    def change_view(self, request, object_id, extra_context=None):       
        self.exclude = ('field', )
        return super(SubSectionAdmin, self).change_view(request, object_id, extra_context)

相关文章