Implemented django-autocomplete-light like such:
views.py:
class VesselAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
user = self.request.user
if not user.is_authenticated:
return Vessel.objects.none()
queryset = Vessel.objects.all()
if self.q:
queryset = queryset.filter(vessel_code__istartswith=self.q)
return queryset
app/urls.py
urlpatterns = [
path(
"vessel-autocomplete/",
VesselAutocomplete.as_view(),
name="vessel-autocomplete",
),
]
project/urls.py
path("vessel/", include("vessel.urls")),
Then rewrote get_formset
method in VesselInline
like such:
admin.py
class VesselInline(admin.TabularInline):
model = Deal.vessels.through
extra = 0
def get_formset(self, request, obj=None, **kwargs):
formset = super().get_formset(request, obj, **kwargs)
formset.form.base_fields['vessel'].widget = autocomplete.ModelSelect2(
url='vessel-autocomplete',
attrs={
'data-placeholder': 'Search for a vessel',
'data-minimum-input-length': 2
}
)
return formset
and now instead of a dropdown I have this autocomplete search field:
Which does the job for now.
Thank you for your help @KenWhitesell!