views:

593

answers:

1

Django creates a search box on the list display page when the field "search_fields" is included in a ModelAdmin.

Some of my inline models refer to the items on the list page. If the search box terms match fields in these inline models, I'd like the results to include the referents in the list.

Example: Database has a table of names and a table of addresses. For each name, there can be several addresses. Each address references a name. When I am searching the list of names, I'd like to be able to enter "Main Street", search the addresses and have the names referred to by those addresses displayed.

I think I would have to override the search function associated with the search box. If that is right, where is that function located?

+3  A: 

I'm not a hundred percent sure I understand your question, but you can search on related fields in the list display by setting search_fields in the admin class:

class MyAdmin(admin.ModelAdmin):
    search_fields = ('name', 'name__address')

This uses the same double-underscore syntax across relations as you would use in a normal filter() call.

Daniel Roseman
Thanks. I don't think that will work for me because there is no explicit address field in a name record. Instead the address records contain a reference to "name" as a foreign key.
Mitch
It took me a while to realize it, but you are right. Thanks again.
Mitch