Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter in django using adding days to existing dates

class Payment(models.Model):
    name = models.CharField()
    payment_date = models.DateField()

Example

Table Payment

id   name  payment_date
1    A     2022-01-01
2    B     2022-01-02
3    C     2022-01-02
4    D     2022-01-06

Suppose the due date for each payment is payment_date+10 days. Suppose iam checking on the date ‘2022-01-12’ the records which are due is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2022-01-01 + 10 days = 2022-01-11 Due
2022-01-02 + 10 days = 2022-01-12 Due
2022-01-06 + 10 days = 2022-01-16 Not Due

So my sample output should be

Sample:
<QuerySet [<Payment: A>, <Payment: B>, <Payment: C>]>

>Solution :

You can do:

from datetime import timedelta
from django.utils import timezone

Payment.objects.filter(
    payment_date__lte=timezone.now() - datetime.timedelta(days=10)
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading