Django admin: exclude field on change form only

后端 未结 4 1610
甜味超标
甜味超标 2020-12-13 16:23

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

4条回答
  •  情话喂你
    2020-12-13 16:41

    The approach below has the advantage of not overriding the object wide exclude property; instead it is reset based on each type of request

    class SubSectionAdmin(admin.ModelAdmin):
        add_exclude = ('field1', 'field2')
        edit_exclude = ('field2',)
    
        def add_view(self, *args, **kwargs):
            self.exclude = getattr(self, 'add_exclude', ())
            return super(SubSectionAdmin, self).add_view(*args, **kwargs)
    
        def change_view(self, *args, **kwargs):
            self.exclude = getattr(self, 'edit_exclude', ())
            return super(SubSectionAdmin, self).change_view(*args, **kwargs)
    

提交回复
热议问题