For some of my models, Django-admin, in the index of the model’s objects, instead of displaying the output of the __unicode__ method like normally, it just displays one of the model’s fields there.
This happens usually when there is something like:
class Meta:
ordering = ['name']
in my model, then the value of the field name is displayed (even though there is also a __unicode__ method), but not always, sometimes it just displays what __unicode__ says even if there is a class Meta ordering.
All my Unicode methods are quite normal, something like:
def __unicode__(self):
return u'[%s] %s' % (self.field, self.name, )
I am puzzled, why is a field used sometimes instead of __unicode__, and how can I make it use the __unicode__ method always? This is Django 1.3. Is this a bug in Django?
You can customise the fields displayed in the django admin using the
list_displayoption in yourModelAdminclass.The model
__unicode__method, and theMeta.orderingoption do not have any affect on the fields displayed.If you do not set
list_display, then the default behaviour is to display a single column with the unicode string for each object.If the unicode string is not displayed for your model, it sounds like you have set
list_display. For example, to display the name field instead of the unicode string, you would do:If you want to display the unicode string and other fields, simply include
__unicode__inlist_display.