要在django中执行左连接吗

要在django中执行左连接吗,django,django-models,django-views,django-filter,django-3.2,Django,Django Models,Django Views,Django Filter,Django 3.2,假设我有两个模型 Model Users: Name: Id: 出席人数 Model Attendance: user: foreign_key(User) present: [present if login else nothing recorded] Date: users = User.objects.all() todays_present = Attendance.objects.filter(date=today) 比如现在 用

假设我有两个模型

Model Users:
       Name:
       Id:
出席人数

Model Attendance:
   user: foreign_key(User)
   present: [present if login else nothing recorded]
   Date:


users = User.objects.all()
todays_present = Attendance.objects.filter(date=today)
比如现在 用户=10 今天(现在)=7

我想找出哪三个用户不在今天的位置。

您可以使用:

User.objects.exclude(出席日期=今天)
如果
日期
字段等于
今天

您可以使用的
字段,则此操作将检索所有无法找到
考勤
的用户:

User.objects.exclude(出席日期=今天)

如果
日期
字段等于
今天
将检索所有无法找到任何
出席人数的用户,这将获得
今天
的实际用户对象(尽管Willem Van Onsem的解决方案更简单)

您可能需要根据您的实际车型相关名称调整“考勤设置”

users_not_present_today = User.objects.annotate(
  today_attendance_count=Count('attendance_set', filter=Q(present=True, date=today)
).filter(today_attendance_count=0)

这将为您获得当前
的实际用户对象(尽管Willem Van Onsem的解决方案更简单)

您可能需要根据您的实际车型相关名称调整“考勤设置”

users_not_present_today = User.objects.annotate(
  today_attendance_count=Count('attendance_set', filter=Q(present=True, date=today)
).filter(today_attendance_count=0)

首先要感谢威廉和阿克斯这两个家伙,他们让我朝着正确的方向思考

我所做的是,首先让所有在场的员工

presents = Attendance.objects.filter(date=today)
然后将其从所有用户中排除,如:

absent = User.objects.exclude(attendance_set__in=presents)

这句话表达了我的愿望……

首先要感谢威廉和阿克斯这两个家伙,他们让我朝着正确的方向思考

我所做的是,首先让所有在场的员工

presents = Attendance.objects.filter(date=today)
然后将其从所有用户中排除,如:

absent = User.objects.exclude(attendance_set__in=presents)
这句话表达了我的愿望