I would like to create a new field named notification_date
using annotate in DjangoORM.
Here is my model:
SpeciesType(models.Model):
# ... some species type setting fields.
heat_lapse = IntegerField()
estrous = IntegerField()
Breeding(models.Model):
# .. some breeding fields
species_type = ForeignKey(SpeciesType, related_name="breedings", on_delete=CASCADE)
created_at = DateTimeField(auto_add_now=True)
Now the formula of date notification of breeding is
Breeding.created_at + (SpeciesType.heat_lapse * SpeciesType.estrous) in days
e.g. 1/29/2017 11:21PM + (3 * 21) in days = 4/2/2017 as notification date
So in order to achieved this i created this query filter with timedelta, F() object, and ExpressionWrapper:
from django.db.models import F, ExpressionWrapper, DateField
from datetime import date, timedelta
Breeding.objects.annotate(
notification_date = ExpressionWrapper(
F('created_at') +
timedelta(days=(
F('species_type__heat_lapse') * F('species_type__estrous')
))
, output_field=DateField())
).filter(
notification_date__lte == date.today()
)
but this wont work because you cannot do an F() inside a timedelta. Anyone knows how to formulate this desired query? it would be a great help for me.