admin管理员组文章数量:1344924
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
本文标签: javascriptDjango Select2 Autocomplete How to Pass Extra Parameter (argId) to the ViewStack Overflow
版权声明:本文标题:javascript - Django Select2 Autocomplete: How to Pass Extra Parameter (argId) to the View? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743784701a2538471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论