Django生成'WHERE ... BETWEEN ...'句子?

时间:2009-09-22 22:27:58

标签: python sql django django-models

实际上,在视图的某个地方:

dif = datetime.timedelta(days=1)
today = datetime.date.today()
yesterday = today - dif
ex = Fact.objects.filter(fecha_fact__lte=today ,fecha_fact__gte=yesterday )

结果是这个SQL查询:

SELECT `facts_fact`.`id` ...
FROM `facts_fact` 
WHERE (`facts_fact`.`fecha_fact` >= 2009-09-21 AND `facts_fact`.`fecha_fact` <= 2009-09-22 ) 

有一种方法可以让Django做一个WHERE BETWEEN而不是&gt; =&lt; =?

:) thx。

2 个答案:

答案 0 :(得分:4)

您是否尝试过range

Fact.objects.filter(fecha_fact__range=(yesterday, today))

答案 1 :(得分:0)

我不知道,但也许(gte / lte):

q1 = Entry.objects.filter(headline__startswith="What")
q2 = q1.exclude(pub_date__gte=datetime.now())
q3 = q1.filter(pub_date__gte=datetime.now())


q = Entry.objects.filter(headline__startswith="What")
q = q.filter(pub_date__lte=datetime.now())