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

Filtering row with same column date value

I have a datetime field, and I want to filter the rows that has the same date value.

models.py

class EntryMonitoring(models.Model):
    student = models.ForeignKey('Student', models.DO_NOTHING)
    clockin = models.DateTimeField()
    clockout = models.DateTimeField(null=True)

views.py

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

def check_attendance(request, nid):
    day = EntryMonitoring.objects.filter(
        clockout__isnull=False, '# same value', student=nid
    ).annotate(...)
    
    # do something

I wanted to add inside that filter query that clockin__date has the same value as clockout__date. Is that possible? If it is, what would be the right query to filter it?

>Solution :

Yes, it is possible. You should use the TruncDate function:

from django.db.models import F
from django.db.models.functions import TruncDate

entries = EntryMonitoring.objects.filter(
    clockout__isnull=False, student=nid, clockin__date=TruncDate(F("clockout"))
).annotate(...)
    
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