Ondrej Sika

Related list display in Django Admin with ordering

07 May 2014

We have simple model:

class Author(models.Model):
    name = models.CharField(max_length=64)

class Book(models.Model):
    title = models.CharField(max_length=64)
    author = models.ForeignKey(Author)

In admin we must create simple function, for list display.

def book_author_name(obj):
    return obj.author.name
book_author_name.admin_order_field = 'author__name'

This function use in ModelAdmin list display:

class BookAdmin(models.ModelAdmin):
    list_display = (
        'title',
        book_author_name,
    )

Share on Facebook, Twitter, Google+, Linkedin

comments powered by Disqus

--