Search code examples
djangodjango-admindjango-admin-filters

Django Inline Autocomplete


Is it possible to filter an inline autocomplete field by a dynamic value entered by a user?

For example, I have a an admin form where staff enters games information including home and visiting team, game date and time, score, etc. They also enter individual player names and stats. I would like to add a filter to show only the players on either the home or visiting team.

I am using the InlineAutocompleteAdmin module, which provides autocomplete hints for input fields.

Here is the current inline autocomplete code:

class IndividualFootballGameInline(InlineAutocompleteAdmin):
    model = IndividualFootballGame
    extra = 1
    related_search_fields = {
        'player': ('player__first_name', 'player__last_name', '#team__sport__sport=Football', '#team__season__season_start_date__year=' + str(get_current_season_start_year('football'))),
    }

If this can be accomplished, can you explain how?


Solution

  • InlineAutocompleteAdmin provides a template that I modified to provide this functionality. The file templates/admin/autocomplete/inline_searchinput.html defines the jQuery lookup() function. I added additional code to check for values in the visiting and home teams field, and to append them to search_fields as needed.