因为需要再数据库里查找指定大于某个月的记录,发现可以使用model中month__gte,很方便,可惜djanogo1.9中才有,做个记录。
Entry.objects.filter(pub_date__month__gte=6)
改成下面的代码之后发现因为自己的是django1.8,报错无此方式,查文档发现该方式是django1.9中才新增的
year = 2012
month = 09
Departure_Date.objects.filter(date_from__year__gte=year,
date_from__month__gte=month,
date_to__year__lte=year,
date_to__month__lte=month)
- 1.8文档
# month¶
# For date and datetime fields, an exact month match. Takes an integer 1 (January) through 12 (December).
Entry.objects.filter(pub_date__month=12)
- 1.9文档
# For date and datetime fields, an exact month match. Allows chaining additional field lookups. Takes an integer 1 (January) through 12 (December).
Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6)
# 相当于如下SQL
SQL equivalent:
SELECT ... WHERE EXTRACT('month' FROM pub_date) = '12';
SELECT ... WHERE EXTRACT('month' FROM pub_date) >= '6';
说点什么
欢迎讨论