Leonardo Graciano
Leonardo Graciano

Reputation: 17

How to filter comparing two informations from the same Django model

I have a MedicalCertificate model like this:

class MedicalCertificate(models.Model):
    needs_leave = models.BooleanField()
    date = models.DateTimeField()
    days_off = models.IntegerField()

And I need to filter the active medical certificates, which are: date + days_off >= datetime.now()

I tried to use F(), but not success. Ex.:

from django.db.models import F

certificates = {
    'needs_leave': True,
    'days_off__gte': datetime.datetime.now() - F('date')
}

queryset = MedicalCertificate.objects.filter(**certificates)

Any idea how to proceed?

Upvotes: 0

Views: 751

Answers (1)

Sunderam Dubey
Sunderam Dubey

Reputation: 8837

You need to filter those medical certificates, which are greater than current date, so you can also query through days:

Basic Condition:

days_off >= date.today().day - date__day

You can query like this:

from datetime import date
from django.db.models import F,Q

MedicalCertificate.objects.filter(
        Q(needs_leave=True) & Q(days_off__gte=date.today().day - F('date__day')))

Upvotes: 1

Related Questions