I have a datetime field, and I want to filter the rows that has the same date value.
models.py
<code>class EntryMonitoring(models.Model):
student = models.ForeignKey('Student', models.DO_NOTHING)
clockin = models.DateTimeField()
clockout = models.DateTimeField(null=True)
</code>
<code>class EntryMonitoring(models.Model):
student = models.ForeignKey('Student', models.DO_NOTHING)
clockin = models.DateTimeField()
clockout = models.DateTimeField(null=True)
</code>
class EntryMonitoring(models.Model):
student = models.ForeignKey('Student', models.DO_NOTHING)
clockin = models.DateTimeField()
clockout = models.DateTimeField(null=True)
views.py
<code>def check_attendance(request, nid):
day = EntryMonitoring.objects.filter(
clockout__isnull=False, '# same value', student=nid
).annotate(...)
# do something
</code>
<code>def check_attendance(request, nid):
day = EntryMonitoring.objects.filter(
clockout__isnull=False, '# same value', student=nid
).annotate(...)
# do something
</code>
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?
Yes, it is possible. You should use the TruncDate
function:
<code>from django.db.models.functions import TruncDate
entries = EntryMonitoring.objects.filter(
clockout__isnull=False, student=nid, clockin__date=TruncDate("clockout")
).annotate(...)
</code>
<code>from django.db.models.functions import TruncDate
entries = EntryMonitoring.objects.filter(
clockout__isnull=False, student=nid, clockin__date=TruncDate("clockout")
).annotate(...)
</code>
from django.db.models.functions import TruncDate
entries = EntryMonitoring.objects.filter(
clockout__isnull=False, student=nid, clockin__date=TruncDate("clockout")
).annotate(...)