Django,按日期范围内的指定月份和年份进行过滤 [英] Django, filter by specified month and year in date range

查看:1809
本文介绍了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))

这是表中的实际数据出发日期

  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


月:9月(09)
年份:2012


想要离开日期结果:

ID 3:触摸09/2012的唯一数据范围



示例2


月份:二月(02)

年份:2013


希望离境日期结果:

ID 1:02/2012于03/2012之前


$ b $那么实际上这一天并不重要。如果月份和年份在date_from和date_to之间,即使是一天,也必须过滤。



我想我必须使用像这个,但我不知道该怎么做。



提前感谢!
Miguel



---编辑---

这是对Aamir Adnan,但它不能像我预期的那样工作,因为从2012年11月到2013年3月,ID 1也必须退回,所以2013年1月在。之间。

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


月份:1
年:2013
其中='%(年)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>]


解决方案

检查文档

  year = 2012 
month = 09
Departure_Date。 object.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'
,输入为
年= 2013
月= 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 %(月)s< MONTH(date_from),%(month)s< MONTH(date_from),%(month)s> = MONTH(date_from))\
AND%(year) < = YEAR(date_to)\
AND%(month)s< = MONTH(date_to)'%\
{'year':year,'month':month}
Departure_Date.objects.extra(where = [where])


I have the following models

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

This is the actual data in the table departure_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

I would like to filter the Destination_Deals if a specified month&year is between date_from and date_to.

Example 1

Month: September (09)
Year: 2012

Wanted departure dates result:
ID 3 : It is the only data range that touch 09/2012

Example 2

Month: February (02)
Year: 2013

Wanted departure dates result:
ID 1 : 02/2012 is before 03/2012

So, the day actually is does not matter. If the month&year is between date_from and date_to, even if it is by one day it must be filter.

I think I must use something like this but I am not sure how to do it.

Thanks in advance! Miguel

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

解决方案

Check the 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)

Alternative method using .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])

There is a specific case where above query does not yield a desired result.

For example:

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

Then %(month)s >= MONTH(date_from) condition is wrong because month 1 is < month 11 in date_from but year is different so MySQL IF condition is required here:

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,按日期范围内的指定月份和年份进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆