How to show all fields of model in admin page?

If you want to include all fields without typing all fieldnames, you can use

list_display = BookAdmin._meta.get_all_field_names()

The drawback is, the fields are in sorted order.

Edit:

This method has been deprecated in Django 1.10 See Migrating from old API for reference. Following should work instead for Django >= 1.9 for most cases -

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

By default, the admin layout only shows what is returned from the object's unicode function. To display something else you need to create a custom admin form in app_dir/admin.py.

See here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

You need to add an admin form, and setting the list_display field.

In your specific example (admin.py):

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'price')
admin.site.register(Book, BookAdmin)

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.


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.


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