Autocomplete fields in Django Admin
Django, Python
autocomplete_fields
is a list of ForeignKey
and/or ManyToManyField
fields you would like to change to autocomplete inputs.
By default, the admin uses a select-box interface (<select>
) for those fields. Sometimes you don’t want to incur the overhead of selecting all the related instances to display in the dropdown.
In the following example, ChoiceAdmin
has an autocomplete field for the ForeignKey
to the Question
. The results are filtered by the question_text
field and ordered by the date_created
field:
py
class QuestionAdmin(admin.ModelAdmin):
ordering = ["date_created"]
search_fields = ["question_text"]
class ChoiceAdmin(admin.ModelAdmin):
autocomplete_fields = ["question"]