Django admin: search for foreign key objects rather than <select>?
Did you take a look at raw_id_fields
?
It should be pretty to close to what you're after.
If you are using Django >= 2.0, you can take advantage of a feature called autocomplete_fields
. You must define search_fields
on the related object’s ModelAdmin because the autocomplete search uses it.
Since you have a ForeignKey
relationship to Asset
in WorkOrder
, in the admin.py
of your app add the following:
from django.contrib import admin
@admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
search_fields = ["serial_number", "asset_tag"]
@admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
autocomplete_fields = ["asset"]
Add the fields you want to use for searching to search_fields
, and add define autocomplete_fields
as shown in the code above.