'Is there a way to annotate this django object computer property?
I have an django Model named Payment that I want to filter by a computed property.
class Payment(Model):
...other fields
terms = PositiveIntegerField(help_text='days')
issued_date = DateField()
then I have this property to calcutate the payment_date
@property
def payment(self):
return self.issued_date + datetime.timedelta(days=self.terms)
I tried to annotate it with something like this:
Payment.objects.annotate(due_date=ExpressionWrapper(F('issued_date') + F('terms'), output_field=DateField()))
but unfortunately it's not working. Any help on this is greatly appreciated.
Solution 1:[1]
Actually this works:
Payment.objects.annotate(due_date=ExpressionWrapper(F('issued_date') + F('terms'), output_field=DateField()))
The problem was that due_date already exist as a property and changing this to a different name fixed the error.
Payment.objects.annotate(date_due=...rest of the expression here)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LearningNoob |
