How to filter distinct year or month queryset in Django models


It simple to get distinct year / month using filter queryset in Django models. We can use dates. For instance:

Get unique year :

1
MyModel.objects.dates(‘created’, ‘year’, order="DESC")

Get unique month :

1
MyModel.objects.dates(‘created’, ‘month’, order="DESC")

Easy right ? 😀


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.