[Django]-Displaying the model's__unicode__ in django admin

48👍

From the list_display documentation there are four things you can add there:

  1. A field
  2. Some method (a callable) that accepts one variable that is the instance for which the row is being displayed.
  3. A string that is the name of a method or attribute defined in the model class.
  4. A string that is the name of a method that is defined in ModelAdmin.

For your case we need #3 for list_display.

For search_fields its easier as you can use follow notation (__) to do lookups.

In the end we come up with this:

class AdultAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'Student_Name',)
    search_fields = ['user__username']

Leave a comment