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?