我有以下型号

class Destination_Deal(models.Model):
    name = models.CharField(_("Nombre"),max_length=200)

class Departure_Date(models.Model):
    date_from= models.DateField(_('Desde'))    
    date_to= models.DateField(_('Hasta'))
    destination_deal = models.ForeignKey(Destination_Deal,verbose_name = _("Oferta de Destino"))

这是表DEFACTION_DATE中的实际数据

id  date_from   date_to     destination_deal_id
1   2012-11-01  2013-03-17  1
2   2012-11-01  2012-12-16  2
3   2012-09-16  2012-10-31  3
4   2012-11-01  2012-12-16  3
5   2013-01-04  2013-01-11  4

如果指定的月份&年份介于日期_from和日期_to之间.

Example 1

月份:9月(09)
年份:2012年

Wanted departure dates result:
ID 3:这是唯一触及09/2012的数据范围

Example 2

月份:2月份(02)
年份:2013年

Wanted departure dates result:
ID 1:02/2012在03/2012之前

所以,这一天其实是无关紧要的.如果月份和年份介于DATE_FROM和DATE_TO之间,即使是在一天之前,也必须是过滤.

我想我必须用this左右,但我不确定怎么做.

提前谢谢!

---Edit---
This is the test for the answer from Aamir Adnan but it is not working as I expected as ID 1 must be also returned because it goes from November 2012 to March 2013, so January 2013 is between.

Departure_Date.objects.all()
[<Departure_Date: id: 1 - from: 2012-11-01 - to: 2013-03-17>,
<Departure_Date: id: 2 - from: 2012-11-01 - to: 2012-12-16>,
<Departure_Date: id: 3 - from: 2012-09-16 - to: 2012-10-31>,
<Departure_Date: id: 4 - from: 2012-11-01 - to: 2012-12-16>,
<Departure_Date: id: 5 - from: 2013-01-04 - to: 2013-01-11>]


month:1
year:2013
where = '%(year)s >= YEAR(date_from) AND %(month)s >= MONTH(date_from) \
    AND %(year)s <= YEAR(date_to) AND %(month)s <= MONTH(date_to)' % \
    {'year': year, 'month': month}
Departure_Date.objects.extra(where=[where])
[<Departure_Date: id: 5 - from: 2013-01-04 - to: 2013-01-11>]

推荐答案

判断documentation

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)

使用.extra的替代方法:

where = '%(year)s >= YEAR(date_from) AND %(month)s >= MONTH(date_from) \
        AND %(year)s <= YEAR(date_to) AND %(month)s <= MONTH(date_to)' % \
        {'year': year, 'month': month}
Departure_Date.objects.extra(where=[where])

在一个特定的情况下,上述查询不会产生期望的结果.

例如:

date_from='2012-11-01'
date_to='2013-03-17'
and input is
year=2013
month=1

那么%(month)s >= MONTH(date_from)个条件是错误的,因为第1个月是<;date_from年第11个月,但年份不同,因此此处需要条件:

where = '%(year)s >= YEAR(date_from) AND IF(%(year)s > YEAR(date_from), \
     IF(%(month)s > MONTH(date_from), %(month)s >= MONTH(date_from), %(month)s < MONTH(date_from)), \
     IF(%(month)s < MONTH(date_from), %(month)s < MONTH(date_from), %(month)s >= MONTH(date_from))) \
     AND %(year)s <= YEAR(date_to) \
     AND %(month)s <= MONTH(date_to)' % \
     {'year': year, 'month': month}
Departure_Date.objects.extra(where=[where])

Django相关问答推荐

Djnago admin中 Select 字段的自定义查询

如何在Django REST框架中实现ForeignKey搜索

try 获取静态文件路径时 Django 给出错误

Django prefetch_related 与 3 个不直接相关的模型

`.objects` 属性在哪里添加到 Django 的 models.Model 类中的实例命名空间?

如何将 select_related 应用于 Django 中的 m2m 关系的对象?

当我告诉它时,如何使用 Django 的记录器来记录回溯?

Django 模型中的隐藏字段

可插拔应用程序的Django默认设置约定?

Python 和 Django OperationalError (2006, 'MySQL server has gone away')

Django - 如何从模型中 Select 特定列?

Django BigInteger自动增量字段作为主键?

使用 sqlite 运行 django 测试

django 模板中对象的模型名称

单击弹出框会滚动回页面顶部 [Bootstrap 和 Django]

如何在保存之前更改 Django 表单字段值?

如何在 Django 中更改上传文件的文件名?

Django中reverse()和reverse_lazy()的区别

Django:根据自定义函数过滤查询

Django - TypeError - save() 得到了一个意外的关键字参数force_insert