Here are my models
class Student:
user = ForeignKey(User)
department = IntegerField()
semester = IntegerField()
...
class Attendance:
student = ForeignKey(Student)
subject = ForeignKey(Subject)
month = IntegerField()
year = IntergerField()
present = IntegerField()
total = IntegerField()
students = Student.objects.filter(semester=semester)
How can I perform a right join between Student and Attendance models, so that I can get a
queryset with all of the students and attendances` if exists for a student, else null?
The documentation mentions left joins but not right joins.
You can use such query:
With
select_relatedyouJOIN'ingAttendancetable. Django does not have explicitjoinORM method, but it doesJOINinternally when you callselect_related. Resulting queryset will contain allStudent's with joined attendances, and when you will callattencande_set.all()on each student - no additional queries will be performed. Check the docs for_setfeature.If you want to query only those students who has at least one attendance, you can use such query: