2021-04-27

Django, use timedelta to add follow-up date to an invoice date

I've got this simple model that I'm using to practice various Django stuff:

class Receivables(models.Model):
    agent = models.ForeignKey(Agents, on_delete=models.CASCADE, related_name='invoices')
    invoice_number = models.CharField(max_length=50)
    invoice_amount = MoneyField(decimal_places=2, default=0, default_currency='USD', max_digits=11)
    invoice_date = models.DateField()
    invoice_due_date = models.DateField()
    total_invoices = models.IntegerField(null=True)

And I'm trying to create a follow-up date that is calculated to be 60 days from the invoice date.

As I understand from researching, I need to use 'annotate' on my queryset, and so I tried this first:

qry = Receivables.objects.annotate(follow_up_date=Receivables.invoice_date+timedelta(days=60))

And that threw a a TypeError for an unsupported operand for +: 'DeferredAttribute' and 'datetime.timedelta'.

I found an anser on StackOverflow (to use an ExpressionWrapper), that I tried to work into my query like this:

qry = Receivables.objects.annotate(follow_up_date=models.ExpressionWrapper(Receivables.invoice_date+timedelta(days=60)))

But that throws the exact same error.

Please help me understand what I'm doing wrong here.



from Recent Questions - Stack Overflow https://ift.tt/3aZ0fH9
https://ift.tt/eA8V8J

No comments:

Post a Comment