I'm using Django with django-autocomplete-light and Select2 to create an autocomplete field. The Select2 field is dynamically added to the page when another field is selected. It fetches data from a Django autocomplete view, and everything works fine.
Now, I need to filter the queryset in my autocomplete view based on an extra parameter (argId). However, I'm not sure how to pass this parameter correctly.
JavaScript (Select2 Initialization)
function getElement(argId) {
let elementSelect = $("<select></select>");
let elementDiv = $(`<div id='element_id' style='text-align: center'></div>`);
elementDiv.append(elementSelect);
$(elementSelect).select2({
ajax: {
url: "/myautocomplete/class",
data: function (params) {
return {
q: params.term, // Search term
arg_id: argId // Pass extra parameter
};
},
processResults: function (data) {
return {
results: data.results // Ensure correct format
};
}
},
placeholder: "Element...",
minimumInputLength: 3
});
return elementDiv;
}
Django Autocomplete View
class ElementAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = MyModel.objects.filter(...)
I want to pass argId from JavaScript to the Django view so that the queryset is filtered accordingly. However, I am not sure if my approach is correct or how to achieve this.
Appreciate any suggestions or improvements. Thanks!
I'm using Django with django-autocomplete-light and Select2 to create an autocomplete field. The Select2 field is dynamically added to the page when another field is selected. It fetches data from a Django autocomplete view, and everything works fine.
Now, I need to filter the queryset in my autocomplete view based on an extra parameter (argId). However, I'm not sure how to pass this parameter correctly.
JavaScript (Select2 Initialization)
function getElement(argId) {
let elementSelect = $("<select></select>");
let elementDiv = $(`<div id='element_id' style='text-align: center'></div>`);
elementDiv.append(elementSelect);
$(elementSelect).select2({
ajax: {
url: "/myautocomplete/class",
data: function (params) {
return {
q: params.term, // Search term
arg_id: argId // Pass extra parameter
};
},
processResults: function (data) {
return {
results: data.results // Ensure correct format
};
}
},
placeholder: "Element...",
minimumInputLength: 3
});
return elementDiv;
}
Django Autocomplete View
class ElementAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = MyModel.objects.filter(...)
I want to pass argId from JavaScript to the Django view so that the queryset is filtered accordingly. However, I am not sure if my approach is correct or how to achieve this.
Appreciate any suggestions or improvements. Thanks!
Share Improve this question asked yesterday CanAnyOneHelpMeCanAnyOneHelpMe 616 bronze badges2 Answers
Reset to default 1Just pass them as query params
/myautocomplete/class?title=title_1
and you can catch them in the class
class ElementAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
title = self.request.GET.get("title")
qs = MyModel.objects.all()
if title is not None:
qs.filter(title__icontains=title)
Filter with:
class ElementAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView):
def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
_arg_id = self.request.GET.get('arg_id'):
if _arg_id is not None:
qs = qs.filter(some_field=_arg_id)
return qs