我想过滤serve_date
模型中的Reserve
字段。
运行此表单时,请返回:expected string or buffer
class Reserve(forms.ModelForm):
food_name = forms.ModelChoiceField(queryset=Food.objects.all())
def __init__(self, year=None, month=None, day=None, serve_date=None, *args, **kwargs):
super(Reserve, self).__init__(*args, **kwargs)
self.year = year
self.month = month
self.day = day
self.serve_date = serve_date
date_stamp = time.strptime(year+month+day,"%Y%M%d")
serve_date = datetime.date(*date_stamp[:3])
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')
食物模型:
class Food(models.Model):
class Meta:
verbose_name = "Food"
verbose_name_plural = "Foods"
def __unicode__(self):
return self.food_name
food_name = models.CharField(verbose_name="Food Name", max_length=50)
price = models.CharField(verbose_name="Food Price", max_length=50)
serve_date = models.DateTimeField(verbose_name="Serve Date")
回溯
args ()
day None
kwargs {u'auto_id': u'id_%s',
u'empty_permitted': True,
u'error_class': <class 'django.forms.utils.ErrorList'>,
u'prefix': u'form-0',
u'use_required_attribute': False}
month None
self <ReservationForm bound=False, valid=Unknown, fields=(food_name;food_count)>
serve_date None
year None