This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

Django’s new Extract*() classes

n1-cz(25)
Published in
#python
Words
55
Reading
1 min
Listen
Play
8y

Django 1.10 and even more 1.11 comes with convenient methods for annotating date parts to your query.

Django comes with these methods in terms of date manipulating

  • ExtractYear
  • ExtractMonth
  • ExtractDay
  • ExtractWeekDay
  • ExtractWeek

and with these in terms of time

  • ExtractHour
  • ExtractMinute
  • ExtractSecond

Then you can go like:

from django.db.models.functions import ExtractWeek
my_query.annotate(week=ExtractWeek(created))

Here is link to the docs.

Thank you!

Django’s new Extract*() classes | Ecency