python - Django,按日期范围内的指定月份和年份过滤

标签 python django

我有以下型号

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"))

这是department_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

如果指定的月份和年份在 date_from 和 date_to 之间,我想过滤 Destination_Deals。

示例 1

Month: September (09)
Year: 2012

想要的出发日期结果:
ID 3:它是唯一触及 09/2012 的数据范围

示例 2

Month: February (02)
Year: 2013

想要的出发日期结果:
ID 1:02/2012 早于 03/2012

所以,日子其实是无所谓的。如果月份和年份在 date_from 和 date_to 之间,即使相差一天也必须过滤。

我想我必须使用 this 之类的东西但我不知道该怎么做。

提前致谢! 米格尔

---编辑---
这是对 Aamir Adnan 答案的测试,但它没有像我预期的那样工作,因为 ID 1 也必须返回,因为它是从 2012 年 11 月到 2013 年 3 月,所以介于 2013 年 1 月之间。

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 个月,但年份不同,所以 MySQL 这里需要 IF 条件:

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])

关于python - Django,按日期范围内的指定月份和年份过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077799/

相关文章:

python - Django 教程(使用 Heroku 上的指南), “Please supply the ENGINE value”(是的,我读过其他线程)

python - 使用 exchangelib 按收件人地址过滤 EWS 邮箱

javascript - 如何计算 Django 模板标签中 for 循环中链接的点击次数?

django - NoReverseMatch at/索引

django - 如何从 django-filters.FilterList 获取请求参数

python - Python 2.5 中的命名元组

python - 大型CSV文件处理

python - Django 。没有名为 reportlab.pdfgen 的模块

python - Django 管理员登录屏幕上出现错误

python - 如何在 Django 中向 URL 添加动态参数