JackLeo JackLeo - 6 months ago 65
Python Question

Django queryset filtering by ISO week number

I have a model that contains

. I'm trying to get query set of that model that contains current week (starts on Monday).

So since Django
contains simple
model I assumed to filter by using
. Logically it's exactly what I want without no extra comparisons and calculations by current week day.

So what I want to do essentially is force
statement to behave in this logic:

if model.date.isocalendar()[2] == datetime.date.today().isocalendar()[2]

Yet how to write it inside filter statement?
will give wrong results (same as comparing to today not this week).

As digging true http://docs.python.org/library/datetime.html I have not noticed any other week day options...

Note from documentation:

date.isocalendar() Return a 3-tuple, (ISO year, ISO week number, ISO


Although I disliked the solution of using ranges yet it's the best option.
However in my case I made a variable that marks the beginning of the week and just look greater or equal value because if I'm looking for a matches for current week. In case of giving the number of the week It would require both ends.

today = datetime.date.today()
monday = today - datetime.timedelta(days=today.weekday())

... \


You're not going to be able to do this. Remember it's not just an issue of what Python supports, Django has to communicate the filter to the database, and the database doesn't support such complex date calculations. You can use __range, though, with a start date and end date.