Issue
I'm trying to get make a complete query set, and now all I need is get the months between two DateTime
fields from my model, it's possible to do this action in a single query set.
Im not talking about filter, cause in the model for example I have two datetimeField()
and now what I want to do is, gets months between this dates.
Solution
If your database supports DurationField
you can go with ExtractMonth:
from django.db import models
from django.db.models.functions import ExtractMonth
queryset = MyModel.objects.annotate(
diff=models.ExpressionWrapper(
models.F('date1') - models.F('date2'), output_field=models.DurationField())
).annotate(months=ExtractMonth('diff'))
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.