我需要在Food
中按日期时间过滤forms.py
模型,但我不知道该怎么做。有谁可以帮助我?
models.py
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)
serve_date = models.DateTimeField(verbose_name="Serve Date")
forms.py
class Reserve(forms.ModelForm):
food_name = forms.ModelChoiceField(
queryset=Food.objects.all(),
widget=forms.Select(),
empty_label=u"برای انتخاب غذا کلیک کنید",
)
class Meta:
model = Reservation
fields = ('food_count', 'food_name')
答案 0 :(得分:1)
您可以通过以下方式使用lte(小于或等于)或gte(大于或等于)来过滤您的查询集: -
queryset=Food.objects.filter(serve_date__lte='2006-01-01')
或
queryset=Food.objects.filter(serve_date__gte='2006-01-01')