【问题标题】:how to obtain a birthday list between two dates with django [duplicate]如何使用django获取两个日期之间的生日列表[重复]
【发布时间】:2015-11-26 14:58:19
【问题描述】:

我正在尝试获取未来 15 天内生日的人员列表。我有这个模型:

class Cliente(models.Model):
    nombre                  = models.CharField(max_length=100)
    fecha_nacimiento        = models.DateField(default=datetime.date.today,blank=True,null=True)
    telefono_numero         = models.CharField(max_length=10, null=True)
    direccion               = models.CharField(max_length=100, null=True)
    otro_contacto           = models.CharField(max_length=150, null=True)

    def __unicode__(self):
        return u'%s' % self.nombre  

我的字段“fecha_nacimiento”(出生日期)是一个models.DateField。当我可以使用它来获取生日在当月的人时:

mes = date.today().month
cumplen_mes = Cliente.objects.filter(fecha_nacimiento__month=mes)

我有一个查询集,在特定月份与谁生日,但我只想查看接下来 15 天内的那些生日。我尝试使用过滤器,但我不知道如何连接过滤器,即:

我可以做到Cliente.objects.filter(fecha_nacimiento__day = a_day)

我阅读了有关 gte 和 lte 的信息,但无法在 fecha_nacimiento 字段上与此过滤器结合使用。

【问题讨论】:

标签: python django


【解决方案1】:

通常,您可以通过将多个关键字参数传递给过滤器来应用多个过滤器,

Cliente.objects.filter(field1='foo', field2='bar') 

或通过链接过滤器调用:

Cliente.objects.filter(field1='foo').filter(field2='bar') 

对于您的具体问题,您可以计算 15 天后的日期。

如果该日期在当月,那么查询是直接的(注意它只适用于 Django 1.9+)

today = date.today()
fifteen_days = today + timedelta(days=15)
Cliente.objects.filter(
    fecha_nacimiento__month=today.month,
    fecha_nacimiento__day__gte=today.day, # we don't want birthdays that have already happened this month
    fecha_nacimiento__day__lte=fifteen_days.day,
)

如果那个日期是下个月,那就更难了。您要么想要本月的日期,要么想要 15 天内的下个月的日期。在 Django 中,您可以使用 Q() 对象进行 OR 查询。

Cliente.objects.filter(
    Q(fecha_nacimiento__month=today.month, fecha_nacimiento__day__gte=fifteen_days.day) |
    Q(fecha_nacimiento__month=fifteen_days.month, fecha_nacimiento__day__lte=fifteen_days.day)
)

综合起来,你有:

today = date.today()
fifteen_days = today + timedelta(days=15)
queryset = Cliente.objects.all()
if today.month == fifteen_days.month:
    queryset = queryset.filter(
        fecha_nacimiento__month=today.month,
        fecha_nacimiento__day__gte=fifteen_days.day,
        fecha_nacimiento__day__lte=fifteen_days.day,
    )
else:
    queryset = queryset.filter(
        Q(fecha_nacimiento__month=today.month, fecha_nacimiento__day__gte=fifteen_days.day) |
        Q(fecha_nacimiento__month=fifteendays.month, fecha_nacimiento__day__lte=fifteen_days.day)
    )

上述查询未经测试,因此可能存在语法错误或不太正确,但我认为该方法可行。

【讨论】:

  • 好的,但不起作用,因为今天是 2015-11-26,15 天后将是 2015-12-11,在我的数据库中,我将 1985-11-27 存储在 fecha_nacimiento 中,并且没有'与过滤器中的条件不匹配。
  • 当我应用第一个选项时,我获得了我所有的客户,因为他们都是在 2015-12-11 之前出生的,在第二个选项中,我获得了所有生日在 11 月的客户,因为出生日期更少比 2015 年 12 月 11 日,在第三种情况下,我获得了一个空查询集,因为在 2015 年 11 月 26 日和 2015 年 12 月 11 日之间没有出生。
  • 这个问题比我最初想象的要难,因为你正在存储出生日期,但要搜索今年的生日。我用不同的方法更新了我的答案。
  • 啊,我忘记了在 Django 1.8 中你不能像 day__gt 这样进行查找。你将能够在 Django 1.9 中做到这一点。
  • 你可以等待 Django 1.9,它应该会在 12 月发布,或者看看 question I linked to above
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 2018-09-17
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
相关资源
最近更新 更多