如何使用自动完成光创建依赖的下拉列表 [英] how to create a dependant drop down using autocomplete light

查看:146
本文介绍了如何使用自动完成光创建依赖的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 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):
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]
pre>

和注册表为
autocomplete_light.register(项目,自动完成)
通过这个,我知道该类别获取值 none (由于我选择的默认值),这种方法也似乎不起作用。



有一种方法可以通过类别的值到 request_for_choices ,以便可以改进serach?

解决方案

code> self.request.POST (或 self.request.GET QueryDict 不会包含比搜索查询更多的信息,因为当创建视图时,它们不会 传递(因此 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 and autocomplete_light. I want to filter the suggestions in the drop down list item 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 of category 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 to request_for_choices so that the serach can be refined?

解决方案

The self.request.POST (or self.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 (so self.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 change getQuery (http://django-autocomplete-light.readthedocs.org/en/stable-2.x.x/script.html#override-autocomplete-js-methods) to append category=foo to the url that is called and then, at choices_for_request read the self.request.GET QueryDictto 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 like self.request.session['category'] = 'foo' and on the choices_for_request you'll get that value.

这篇关于如何使用自动完成光创建依赖的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆