I am using django autocomplete_light to autocomplete the value inserted on a ForeignKeyField. I want to apply extra filtering on the suggestions based upon the arguements passed to__init__ method.

This is my form

class CamenuForm(autocomplete_light.ModelForm):
  class Meta:
   model = Ca_dispensaries_item
   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)
    for field_name in self.fields:
        field = self.fields.get(field_name)
        if field:
            field.widget.attrs.update({
                'placeholder': field.label,
                'class': 'form-control'
                })

Here is the registry

autocomplete_light.register(Items, search_fields=('item_name',))

Here is this line in the __init__ method which doesnt seems to work with autocomplete. Though I used filter over here, yet the suggestions are not filtered.

self.fields['item'].queryset=Items.objects.filter(product_type__name=self.category)
0

There are 0 best solutions below