How to extend django admin select?

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

Some of my built-in models relate to items on the list page. If the search fields match the fields in these inline models, I would like the results to include the referents in the list.

Example. The database has a table of names and a table of addresses. There can be several addresses for each name. Each address refers to a name. When I search for a list of names, I would like to be able to enter "main street", search for addresses, and list the names indicated by those displayed addresses.

I think I would have to override the search function associated with the search box. If that's correct, where is this function?

+1


a source to share


1 answer


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

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

      



In this case, the same double-underscore relationship syntax is used that you would use in a normal filter () call.

+3


a source







All Articles