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
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(...)