How to show all fields of model in admin page?

2020-05-14 14:06发布

here is the models page

In this picture, only the title shows up on here, I used:

 def __unicode__(self):
        return self.title;  

here is the each individual objects

How do I show all these fields?

How do I show all the fields in each Model page?

9条回答
老娘就宠你
2楼-- · 2020-05-14 14:21

Show all fields:

list_display = [field.attname for field in BookModel._meta.fields]
查看更多
▲ chillily
3楼-- · 2020-05-14 14:23

If you want to include all but the ManyToManyField field names, and have them in the same order as in the models.py file, you can use:

list_display = [field.name for field in Book._meta.fields if field.name != "id"]

As you can see, I also excluded the id.

If you find yourself doing this a lot, you could create a subclass of ModelAdmin:

class CustomModelAdmin(admin.ModelAdmin):

    def __init__(self, model, admin_site):
        self.list_display = [field.name for field in model._meta.fields if field.name != "id"]
        super(CustomModelAdmin, self).__init__(model, admin_site)

and then just inherit from that:

class BookAdmin(CustomModelAdmin):
    pass

or you can do it as a mixin:

class CustomModelAdminMixin(object):

    def __init__(self, model, admin_site):
        self.list_display = [field.name for field in model._meta.fields if field.name != "id"]
        super(CustomModelAdminMixin, self).__init__(model, admin_site)

class TradeAdmin(CustomModelAdminMixin, admin.ModelAdmin):
    pass

The mixin is useful if you want to inherit from something other than admin.ModelAdmin.

查看更多
姐就是有狂的资本
4楼-- · 2020-05-14 14:25

Here is my approach, will work with any model class:

MySpecialAdmin = lambda model: type('SubClass'+model.__name__, (admin.ModelAdmin,), {
    'list_display': [x.name for x in model._meta.fields],
    'list_select_related': [x.name for x in model._meta.fields if isinstance(x, (ManyToOneRel, ForeignKey, OneToOneField,))]
})

This will do two things:

  1. Add all fields to model admin
  2. Makes sure that there is only a single database call for each related object (instead of one per instance)

Then to register you model:

admin.site.register(MyModel, MySpecialAdmin(MyModel))

Note: if you are using a different default model admin, replace 'admin.ModelAdmin' with your admin base class

查看更多
老娘就宠你
5楼-- · 2020-05-14 14:28

Many of the answers are broken by Django 1.10. For version 1.10 or above, this should be

list_display = [f.name for f in Book._meta.get_fields()]

Docs

查看更多
ら.Afraid
6楼-- · 2020-05-14 14:28

I like this answer and thought I'd post the complete admin.py code (in this case, I wanted all the User model fields to appear in admin)

from django.contrib import admin
from django.contrib.auth.models import User
from django.db.models import ManyToOneRel, ForeignKey, OneToOneField


MySpecialAdmin = lambda model: type('SubClass'+model.__name__, (admin.ModelAdmin,), {
    'list_display': [x.name for x in model._meta.fields],
    'list_select_related': [x.name for x in model._meta.fields if isinstance(x, (ManyToOneRel, ForeignKey, OneToOneField,))]
})

admin.site.unregister(User)
admin.site.register(User, MySpecialAdmin(User))
查看更多
仙女界的扛把子
7楼-- · 2020-05-14 14:29

I found OBu's answer here to be very useful for me. He mentions:

The drawback is, the fields are in sorted order.

A small adjustment to his method solves this problem as well:

list_display  = [f.name for f in Book._meta.fields]

Worked for me.

查看更多
登录 后发表回答