2
我想更多的參數形式Django的 - 如何添加額外的參數表格,查詢集
class Reserve(forms.ModelForm):
food_name = forms.ModelChoiceField(queryset=Food.objects.all())
def __init__(self, year=None, month=None, day=None, *args, **kwargs):
super(Reserve, self).__init__(*args, **kwargs)
self.year = kwargs.pop('year')
self.month = kwargs.pop('month')
self.day = kwargs.pop('day')
self.fields['food_name'].queryset = Food.objects.filter(
serve_date__year = year, serve_date__month = month, serve_date__day = day)
class Meta:
model = Reservation
fields = ('food_count', 'food_name')
但其回報KeyError異常添加到查詢集:
'year'