I have a django admin tabular inline, in which I have form = ProdForm
which holds a modelchoicefield
select box as follows;
class ProdForm(forms.ModelForm):
productid = forms.ModelChoiceField(queryset=Product.objects.filter(active=True),
widget=Select2(select2attrs={"width": "400px"}), )
as you can see, I am using the easy_select2 module, that is enabling providing me with a look up field too.
However, if I try to load this in the corresponding tabularInLine, it never loads because there is a very high number of records (suppose millions). Therefore loading the entire queryset is not possible. I need to find a way to do this so people using the admin can search the object they need, suppose by a name which is one of the attributes on the Product model
.
An idea is to keep the search box but not load the queryset initially and hit the db when there are, for example 3 or more letters in the search field, that could maybe work. However, that would include some js that I am not really familiar with and I would prefer some pythonic/django way of doing this.
Or maybe there is a nice django way but I haven't found it and I am at my wits end. I would appreciate any suggestions.
Try to use ajax.
You can call
ajax
when you submit search bar, next search your record inview.py
, and at the end display result in console or template.This is a general example:
file.js
view.py
Read here more about ajax:
CFE
pluralsight
geeksforgeeks