Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming a model like this--</p> <pre><code>class Person(models.Model): name = models.CharField(max_length=40) birthday = models.DateTimeField() # their next birthday </code></pre> <p>The next step would be to create a query filtering out any records with birthdays having a month and day in between (now.month, now.day) and (then.month, then.day). You can actually access the month and day attributes of the datetime object using the queryset API by passing Person.objects.filter a keyword argument like this: "birthday__month." I tried this with an actual queryset API method like "birthday__month__gte" and it failed though. So I would suggest simply generating a literal list of month/day tuples representing each (month, day) in the date range you want records for, then compose them all into a query with django.db.models.Q, like so:</p> <pre><code>from datetime import datetime, timedelta import operator from django.db.models import Q def birthdays_within(days): now = datetime.now() then = now + timedelta(days) # Build the list of month/day tuples. monthdays = [(now.month, now.day)] while now &lt;= then: monthdays.append((now.month, now.day)) now += timedelta(days=1) # Tranform each into queryset keyword args. monthdays = (dict(zip(("birthday__month", "birthday__day"), t)) for t in monthdays) # Compose the djano.db.models.Q objects together for a single query. query = reduce(operator.or_, (Q(**d) for d in monthdays)) # Run the query. return Person.objects.filter(query) </code></pre> <p>After debugging, this should return a queryset with each person who has a birthday with month and day equal to any of the months or days in the specified list of tuples.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload