44👍
✅
Either construct custom SQL or use
date_list = Note.objects.all().dates('created', 'year')
for years in date_list:
Note.objects.filter(created__year = years.year)
This is the way it is done in date based generic views.
- [Django]-TypeError: data.forEach is not a function
- [Django]-How to use Cassandra in Django framework
- [Django]-Django serve static index.html with view at '/' url
1👍
We can improve the filter using a list of years, then check if the year of creation is on that.
With that list, the database will be called once one time, because the Note.objects.filter
will runs outside the for
loop.
Something like this:
notes = Note.objects.all().dates('created', 'year')
years = [note.year for note in notes]
Note.objects.filter(created__year__in = years)
- [Django]-Are Django SECRET_KEY's per instance or per app?
- [Django]-Django content types – how to get model class of content type to create a instance?
- [Django]-Django: Unable to load template tags
Source:stackexchange.com