如何使用自动完成光创建依赖的下拉列表 [英] how to create a dependant drop down using autocomplete light
问题描述
我已经使用 Django模型表单
和 autocomplete_light
创建了一个表单。我想根据调用该类时传递的争论过滤掉下拉列表项目
中的建议。
我的表单是
class CamenuForm(autocomplete_light.ModelForm):
class Meta:
model = Ca_dispensaries_item
exclude =('recipary',)
autocomplete_fields =('item',)
def __init __(self,* args,** kwargs):
self .category = kwargs.pop('category',None)
super(CamenuForm,self).__ init __(* args,** kwargs)
self.fields ['item']。queryset = Items。 objects.filter(product_type__name = self.category)
我已经在 __ init __
根据类别
的值传递,但它似乎不起作用。
注册表是
autocomplete_light.register(Items,search_fields = ('item_name',))
表单被调用为
form = CamenuForm(request.POST或None,category = category)
请给我一个方法,以便我可以根据调用表单中传递的值来改进搜索。
我已经尝试修改它通过使用
class AutoComplete(autocomplete_light.AutocompleteModelBase):
pre>
search_fields =('item_name',)
def choices_for_request(self):
category = self.request.POST.get('category','none')
打印类别
choices = self.choices.all()
如果类别:
choices = choices.filter(product_type__name = category)
return self.order_choices(choices)[0:self.limit_choices]
和注册表为
autocomplete_light.register(项目,自动完成)
通过这个,我知道该类别获取值none
(由于我选择的默认值),这种方法也似乎不起作用。
有一种方法可以通过
类别
的值到request_for_choices
,以便可以改进serach?解决方案code> self.request.POST (或
self.request.GET
)QueryDict $ c $自动完成类别不会包含比搜索查询更多的信息,因为当创建视图时,它们不会 传递(因此
self)。 request.POST.get('category','none')
将始终返回'none'
)。
所以困难的部分是以某种方式将一个参数(
category
)传递给完全不同的视图。这可以通过修改调用自动完成的javascript来完成。意思是,您需要更改getQuery
( http://django-autocomplete-light.readthedocs.org/en/stable-2.xx/script.html#override-autocomplete-js-方法)将category = foo
追加到被调用的URL,然后在choices_for_request
读取self.request.GET
QueryDict
以获取该值。
另一种方法是将类别参数放在会话中,然后在
choices_for_request
中读取会话。例如,在您的视图的__ init __
中,您将执行类似self.request.session ['category'] ='foo'
和choices_for_request
,您将得到该值。I have created a form using
Django Model Form
andautocomplete_light
. I want to filter the suggestions in the drop down listitem
according to the arguement passed when the class is called.My form is
class CamenuForm(autocomplete_light.ModelForm): class Meta: model = Ca_dispensaries_item exclude = ('dispensary',) autocomplete_fields = ('item',) def __init__(self, *args, **kwargs): self.category = kwargs.pop('category', None) super(CamenuForm, self).__init__(*args, **kwargs) self.fields['item'].queryset=Items.objects.filter(product_type__name=self.category)
I have applied a filter in
__init__
according to the value ofcategory
passed but it doesnot seems to work.The registry is
autocomplete_light.register(Items,search_fields=('item_name',))
And the form is called as
form = CamenuForm(request.POST or None, category=category)
Please suggest me a way so that I can refine the search based on the value passed while calling form.
I have tried to modify it by using
class AutoComplete(autocomplete_light.AutocompleteModelBase): search_fields=('item_name',) def choices_for_request(self): category = self.request.POST.get('category', 'none') print category choices = self.choices.all() if category: choices = choices.filter(product_type__name=category) return self.order_choices(choices)[0:self.limit_choices]
and registry as autocomplete_light.register(Items, AutoComplete ) Through this, I get to know that category gets the value
none
(because of the default value I chose) and this method also doesnt seems to work.IS there a way that the value of
category
can be passed torequest_for_choices
so that the serach can be refined?解决方案The
self.request.POST
(orself.request.GET
)QueryDict
of an Autocomplete class will not contain any more information than the search query because they are not passed when the view is created (soself.request.POST.get('category', 'none')
will always return'none'
).So the difficult part is to somehow pass an argument (
category
) to a completely different view. This could be done, for instance by modifying the javascript that calls the autocomplete. Meaning, you'll need to changegetQuery
(http://django-autocomplete-light.readthedocs.org/en/stable-2.x.x/script.html#override-autocomplete-js-methods) to appendcategory=foo
to the url that is called and then, atchoices_for_request
read theself.request.GET
QueryDict
to get that value.Another way to do it is to put the category parameter to your session and then read the session in the
choices_for_request
. For example, on the__init__
of your view you'll do something likeself.request.session['category'] = 'foo'
and on thechoices_for_request
you'll get that value.这篇关于如何使用自动完成光创建依赖的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!