How to hide some fields in django-admin?

后端 未结 4 1997
猫巷女王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 00:52

    You are to create admin.py in your module (probably book)

    class BookAdmin(admin.ModelAdmin):
        list_display = ("pk", "get_title_or_nothing")
    

    In Book class:

    class Book:
        ...
        def get_title_or_nothing(self):
            if self.type == WEIRD_TYPE:
                return ""
            return self.title
    

    UPDATED:

    class BookAdmin(admin.ModelAdmin):
        list_display = ("pk", "get_title_or_nothing")
    
        def get_form(self, request, obj=None, **kwargs):
            if obj.type == "1":
                self.exclude = ("title", )
            form = super(BookAdmin, self).get_form(request, obj, **kwargs)
            return form
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-01 01:09

    Apropos @Lorenz @mrts answer

    with Django 2.1 I found that exclude does not work if the field is already specified via fields = .

    In that case you may use

    self.fields.remove('title')
    

    fields will have to be defined as a list [] for this to work

    0 讨论(0)
  • 2021-02-01 01:16

    For Django > 1.8 one can directly set the fields to be excluded in admin:

     class PostCodesAdmin(admin.ModelAdmin):
          exclude = ('pcname',)
    

    Hidden fields are directly defined in Django's ORM by setting the Field attribute: editable = False

    e.g.

    class PostCodes(models.Model):
      gisid  = models.IntegerField(primary_key=True)
      pcname = models.CharField(max_length=32, db_index=True, editable=False)
      ...
    

    However, setting or changing the model's fields directly may not always be possible or advantegous. In principle the following admin.py setup could work, but won't since exclude is an InlineModelAdmin option.

    class PostCodesAdmin(admin.ModelAdmin):
         exclude = ('pcname',)
    ....
    

    A solution working at least in Django 1.4 (and likely later version numbers) is:

    class PostCodesAdmin(admin.ModelAdmin):
      def get_form(self, request, obj=None, **kwargs):
          form = super(PostCodesAdmin, self).get_form(request, obj, **kwargs)
          del form.base_fields['enable_comments'] 
          return form
    

    For the admin list-view of the items, it suffices to simply leave out fields not required: e.g.

    class PostCodesAdmin(admin.ModelAdmin):
      list_display = ('id', 'gisid', 'title', )
    
    0 讨论(0)
提交回复
热议问题