Can “list_display” in a Django ModelAdmin display attributes of ForeignKey fields?

As another option, you can do look ups like:

class UserAdmin(admin.ModelAdmin):
    list_display = (..., 'get_author')
    
    def get_author(self, obj):
        return obj.book.author
    get_author.short_description = 'Author'
    get_author.admin_order_field = 'book__author'

Since Django 3.2 you can use display() decorator:

class UserAdmin(admin.ModelAdmin):
    list_display = (..., 'get_author')
    
    @display(ordering='book__author', description='Author')
    def get_author(self, obj):
        return obj.book.author

Leave a Comment