You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django ORM筛选clockin与clockout日期相同的EntryMonitoring数据行

Django 出勤记录查询实现

这完全可行,直接通过Django ORM的filter方法搭配F()表达式就能满足需求,无需额外复杂操作。

正确的查询语句示例:

from django.db.models import F
from .models import EntryMonitoring

def check_attendance(request, student_nid):
    # 筛选符合条件的出勤记录
    valid_records = EntryMonitoring.objects.filter(
        student__nid=student_nid,
        clockout__isnull=False,
        clockin__date=F('clockout__date')
    )
    # 后续业务逻辑处理...

各条件说明:

  • student__nid=student_nid:匹配指定nid的学生关联记录(这里假设Student模型的唯一标识字段为nid)
  • clockout__isnull=False:排除clockout字段为空的记录,确保只筛选完成签退的记录
  • clockin__date=F('clockout__date'):借助Django的F()表达式,直接在数据库层面对比clockin和clockout的日期部分是否一致,避免将数据拉取到内存中处理,保证查询效率

内容的提问来源于stack exchange,提问作者zheni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 18:40:53