Search code examples
djangodjango-modelsdjango-viewsdjango-filterdjango-3.2

Want to perform left join in django


Say I've 2 models

Model Users:
       Name:
       Id:

And attendance

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)

Now for instance Users = 10 todays_present = 7

I want to find which three users are not in todays_present.


Solution

  • You can work with .exclude(…) [Django-doc] with:

    User.objects.exclude(attendance__date=today)

    This will retrieve all users for which no Attendance can be found where the date field is equal to today.