Django Model Form
と を使用してフォームを作成しましたautocomplete_light
。item
クラスが呼び出されたときに渡された引数に従って、ドロップダウン リストの提案をフィルター処理したいと考えています。
私のフォームは
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)
渡さ__init__
れた値に従ってフィルターを適用しましたが、機能していないようです。category
レジストリは
autocomplete_light.register(Items,search_fields=('item_name',))
そして、フォームは次のように呼び出されます
form = CamenuForm(request.POST or None, category=category)
フォームの呼び出し中に渡された値に基づいて検索を絞り込む方法を教えてください。
を使用して変更しようとしました
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]
およびレジストリとして autocomplete_light.register(Items, AutoComplete ) これにより、カテゴリが値を取得することがわかりnone
(選択したデフォルト値のため)、このメソッドも機能しないようです。
検索を洗練できるように値をcategory
渡す方法はありますか?request_for_choices