I want to get last 12 months applicants data in months wise view. If this is March then i need to get data of [3,2,1,12,...,4]
in this scenario. Here is my model:
class Applicant(models.Model):
full_name = models.CharField(max_length=200, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
.....
While i try query:
Applicant.objects.values('created_at__month')annotate(total = Count('created_at__month'))
it return values like [{'created_at__month': 2, 'total': 3}, {'created_at__month': 3, 'total': 13}]
.
If I have 2022's month March total applicant 22 and 2021's month March total applicant 20, this query return value 42 in created_at__month: 3
, but I need only past 12 months data from this month.
How can I write this query efficiently?