How to hide some fields in django-admin?

后端 未结 4 2004
猫巷女王i
猫巷女王i 2021-02-01 00:43
class Book(models.Model):
    title = models.CharField(..., null=True)
    type = models.CharField(...)
    author = models.CharField(...)

I have a sim

4条回答
  •  长情又很酷
    2021-02-01 01:03

    I tried to override get_form() function but some mix up errors occur when I switch in different records. I found there is a get_exclude() function we can override.

    Use:

    class BookAdmin(admin.ModelAdmin):
        def get_exclude(self, request, obj=None):
            if obj and obj.type == "1":
                # When you create new data the obj is None
                return ("title", )
            return super().get_exclude(request, obj)
    

提交回复
热议问题