40👍
✅
Use __range
. You’ll need to actually calculate the beginning and end of the week first:
import datetime
date = datetime.date.today()
start_week = date - datetime.timedelta(date.weekday())
end_week = start_week + datetime.timedelta(7)
entries = Entry.objects.filter(created_at__range=[start_week, end_week])
11👍
Since Django 1.11, we you can use week
Field lookup:
Entry.objects.filter(created_at__week=current_week)
It will give you the week from monday to sunday, according to ISO-8601.
To query for the current week:
from datetime import date
current_week = date.today().isocalendar()[1]
isocalendar()
will return a tuple with 3 items: (ISO year, ISO week number, ISO weekday).
- Django + uWSGI + Nginx + SSL – request for working configuration (emphasis on SSL)
- Django static files on heroku
- Django rest framework nested viewsets and routes
- How to filter filter_horizontal in Django admin?
2👍
Yep, this question is at 2 years ago. Today with more experiences, I recommend using arrow
with less pain in handling date time.
Checkout: https://github.com/crsmithdev/arrow
- Django models across multiple projects/microservices. How to?
- 'WSGIRequest' object has no attribute 'session' while upgrading from django 1.3 to 1.9
- Why does Django South 1.0 use iteritems()?
- Can Django run on Gunicorn alone (no Apache or nginx)?
Source:stackexchange.com