Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin raw_id_field popup with search box

I have foreign key field with multiple items and it's really hard to find needed values.

Is there any solution to add search to admin's raw_id_fields popup? Or any alternative from core django package?

If there is no solution I'll use this one (https://github.com/yourlabs/django-autocomplete-light) but I want to avoid external dependencies.

like image 616
Alex T Avatar asked Oct 20 '25 18:10

Alex T


1 Answers

Suppose you have two models Book and Publisher and you have

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)

Then in your admin.py you have to create a custom admin class for Publisher if you haven't already done so.

class Publisher(admin.ModelAdmin):
    ...
    search_fields = ('name','address','city')

Now in the raw_id_field popup for Book model instances in the admin, you will see that a search box appears (this is so in django 1.9 at any rate)

like image 190
e4c5 Avatar answered Oct 22 '25 09:10

e4c5