2

In a Django 3.0 app, I have a Donor Model with a foreign key to the donor's congressional district:

# models.py
class Donor(models.Model):
    """A class to represent an individual donor"""
    
    name = models.CharField(
        help_text = "Donor's name"
    )
    district = models.ForeignKey(
        District,
        help_text = "Donor's congressional district"
    )

class District(models.Model):
    """A class to represent a U.S. congressional district"""
    
    dist_name = models.CharField(
        help_text = "District name"
    )

In the Admin Add page for Donor, there's a drop-down selection menu for District. The problem is that there are 435 congressional districts in the U.S. That's an awful lot to scroll through each time I add a Donor, so I'd like to add a search box.

Django has the ability to define search fields. I tried to do so:

# admin.py

@admin.register(Donor)
class DonorAdmin(admin.ModelAdmin):
    search_fields = ['district']

This did not affect the Donor Add page. 'District' still shows up as an enormous drop-down, and there are no search features anywhere on the page.

The search_fields documentation says specifically

Set search_fields to enable a search box on the admin change list page.

Emphasis mine: search_fields seems to apply to only the Change List page, not the Add page. I assume this is why my attempt didn't work.

Is it possible to add a search box to a Django Admin Add page?

2 Answers 2

1

Yes, you can add a search box to Django admin pages.

Add this to your admin class:

search_fields = ['name', 'author',.......] #add the fields you want to search

And the search box it will appear

Sign up to request clarification or add additional context in comments.

Comments

0

No you can't add a search box to django admin pages.

2 Comments

Thanks for a direct response. Can you point me toward any documentation or other information that would let me verify this?
I will when I get or see one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.