Listing and Searching fields in another table

208 views
Skip to first unread message

Iyanuoluwa Loko

unread,
Oct 5, 2021, 4:37:22 PM10/5/21
to Django users

Good day, I would like to use the list_display and search_fields functions in one table on the Django admin page to display and search for a field in another table. Could I please get any pointers on how to go about this?

 

Derek

unread,
Oct 6, 2021, 9:55:04 AM10/6/21
to Django users
Obviously you will be searching in related table. 

So if you have models.py like:

class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

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

Then in your admin.py for book:

    search_fields = (
        'title', 'author__first_name',  'author__last_name', )

HTH
Reply all
Reply to author
Forward
0 new messages