B
B
bQ12019-08-14 10:00:46
Django
bQ1, 2019-08-14 10:00:46

How to get posts for current week django queryset?

How to get posts for current week django queryset?
Tried through timedelta but it turns out not what you need. For example today is Monday and I need to get posts only from this week i.e. from this Monday, and if you use timedelta, it turns out that posts are also taken from last week starting from Tuesday

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-08-14
@bQ1

You can request data by week number week and iso_year

import datetime
date = datetime.datetime.today()
week = date.strftime("%V")

Entry.objects.filter(pub_date__week=week)

from django.utils.timezone import now
year, week, _ = now().isocalendar()

Entry.objects.filter(pub_date__iso_year=year, pub_date__week=week)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question