Django: Total birthdays each day for the next 30 days

后端 未结 4 941
遇见更好的自我
遇见更好的自我 2021-01-03 16:35

I\'ve got a model similar to this:

class Person(models.Model):
    name = models.CharField(max_length=40)
    birthday = DateTimeField() # their next birthda         


        
4条回答
  •  被撕碎了的回忆
    2021-01-03 17:08

    from django.db.models import Count
    import datetime
    today = datetime.date.today()
    thirty_days = today + datetime.timedelta(days=30)
    birthdays = dict(Person.objects.filter(
                        birthday__range=[today, thirty_days]
                     ).values_list('birthday').annotate(Count('birthday')))
    
    
    for day in range(30):
        date = today + datetime.timedelta(day)
        print "[%s, %s]" % (date, birthdays.get(date, 0))
    

提交回复
热议问题