我有以下型號:如何篩選日期時間字段上的對象?
def announcements(request):
announcements = Announcement.objects.all().filter(
is_active = True
)
return HttpResponse(
serializers.serialize("json", announcements))
這工作得很好:
class Announcement(models.Model):
name = models.CharField(max_length=500)
content = models.CharField(max_length=1000)
is_active = models.BooleanField(default=False)
date_start = models.DateTimeField()
date_end = models.DateTimeField()
def __str__(self):
return self.name
我可以像is_active
字段進行篩選的對象。但是,當我要過濾這些基於date_start
場這樣的:
def announcements(request):
announcements = Announcement.objects.all().filter(
date_start >= datetime.now()
)
return HttpResponse(
serializers.serialize("json", announcements))
我得到global name 'date_start' is not defined
錯誤。
可能的重複[如何過濾Django中DateTimeField的日期?](http://stackoverflow.com/questions/1317714/how-can-i-filter-a-date-of-a-datetimefield -in-django) –
在使用filter()之前,不需要調用.all(),因爲filter()被代理到queryset。 –