How to use order_by and annotate in Django to retrieve products based on the order of a season?

Assuming I got a Product model. I use Django-taggit to allow me to add tags for each product. In each product I will add tags like winter, summer, autumn, spring. Then I create functions like this_month() using timezone.now for time aware in Django. The this_month() function would return an integer such as 8 for august since I would be using .strftime(). Now I would create a this_season(this_month) function which accepts only an integer; this function would return a string such as 'winter'.

So, to sum it up, I can call this_season(this_month) to get the correct season such as if now = timezone.now, this_month = now.strftime('%m'), and season = this_season(this_month) - if this_month is an integer of 8 - then this_season() would return 'summer'.

The question is how do I query the database using annotate to create a pseudo column that allows me to retrieve the products that order_by tags (using tags of Django-taggit) based on the season? So, the results would return with products that in the correct season in descending order - meaning the product of the season Summer in August would show at the top of the list.

Any clue?

Back to Top