我的项目有多个模型和模型的自定义管理页面。所有模型都继承自 BaseModel。
我在 BaseModel 中添加了 2 个新字段。鉴于所有模型都继承自 BaseModel,它们也获得了这 2 个新字段,现在它们显示在管理页面中。但我不希望这两个字段显示在管理页面中。
要从管理页面中排除字段,我们通常在管理类中使用excludes = [要排除的字段]。
有没有办法在不触及所有管理类的情况下实现这一点?
请您参考如下方法:
使用 editable字段选项。
Field.editable
If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.
解决方案是为这两个字段设置 editable=False。
像这样:
class BaseModel(models.Model):
...
new_field_1 = models.IntegerField( # Assumed that it is an integer field.
editable=False,
...
)
new_field_2 = models.IntegerField( # Assumed that it is an integer field.
editable=False,
...
)
