I'm following along in the DAL documentation to add a filtered field to my form, but the forwarding isn't working to connect one field to the other:
Forms.py
class PurchaseForm(forms.ModelForm):
commodity = forms.ModelChoiceField(
queryset=Commodity.objects.all(),
widget=autocomplete.ModelSelect2(url='commodity-autocomplete'),
required=False,
)
class Meta:
model = Purchase
fields = ["variety"]
widgets = {
'variety': autocomplete.ModelSelect2(url='variety-autocomplete', forward=['commodity'],
}
Views.py
class VarietyAutocompleteView(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Variety.objects.all()
commodity = self.forwarded.get('commodity', None)
print("Commodity:" + str(commodity))
if commodity:
qs = qs.filter(commodity=commodity)
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
I'd like my Variety choices to be filtered by their foreign key relationship to Commodity objects. Both autocomplete fields are working on their own just fine, but the choice from the commodity
field is not being forwarded to the VarietyAutocompleteView
(my print command prints Commodity:None
). Is this perhaps because I am passing a foreign key object? Or have I set this up incorrectly somehow?