Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show all fields of model in admin page?

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?

like image 869
iCodeLikeImDrunk Avatar asked May 10 '12 22:05

iCodeLikeImDrunk


People also ask

How do I add an administrator to a model?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.


1 Answers

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()] 
like image 87
OBu Avatar answered Sep 22 '22 02:09

OBu