参考Performing a right join in django,当我尝试类似的方法(字段略有不同)时:
class Student:
user = ForeignKey(User)
department = IntegerField()
semester = IntegerField()
class Attendance:
student_attending = ForeignKey(Student, related_name='attendee')
subject = ForeignKey(Subject)
当我运行此查询时:
queryset = Student.objects.all().select_related('attendance_set')
我收到了这个回复:
django.core.exceptions.FieldError: Invalid field name(s) given in select_related: 'attendance_set'.
什么可以触发该警告,如何让“加入”正常工作?
答案 0 :(得分:2)
这个相关问题的公认答案很简单,错误 - 作为评论有注释。
select_related
仅适用于前向关系。对于向后的,您需要prefetch_related
:
Student.objects.all().prefetch_related('attendance_set')
请注意,这将执行两个单独的查询。