我想对 django admin 隐藏一个列并从 session 中设置它的值。
简单来说,我想从 session 中设置一个模型的属性,并且不想在管理中为该列设置一个字段。
有人可以帮助我吗?
最佳答案
您可以 mark the field as read-only但它仍然在管理员中可见,或者 completely exclude it .
class MyModel(models.Model):
field1 = models.CharField(max_length=20) # this is editable
field2 = models.CharField(max_length=20, editable=False) # this is not
或
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
exclude = ['field2']
如果您在管理员中使用自定义模型表单,只需 don't include the field in the fields
attribute .
关于django - 从 Django admin 中隐藏模型属性并手动设置其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035133/