Filter using @property field django
I would like to filter based on @property field to generate a queryset. My model as below.
`class PrHW(models.Model):
po_duration = models.IntegerField(null=True)
provision_start_date = models.DateField(null=True, blank=True)
description = models.TextField(blank=True, null=True)
@property
def provision_end_date(self):
provision_end_date = self.provision_start_date + relativedelta(months=self.po_duration)
return provision_end_date`
Since "provision_end_date" is a calculated field, it is not part of PrHW.objects.all() and hence I am unable create queryset using it as a filter option (eg, generate list of PrHW objects where provision_end_date is less than today - This generates error that "provision_end_date" is not a valid field; which was expected). I tried creating custom model manager but still I am unable to access either the @property field or other fields such as "provision_start_date" in the custom manager. May be this would be straingt forward but even after several searches, unable to get the hang of it. Any help is appreciated.
The @property decorator allows you to access the model field as if it was a regular property.
This means you can call provision_start_date as if it were a variable:
start_date = prwh.provision_start_date
Also, you can do the checks like this
if prwh.provision_start_date < today: "return something"