Issue
students = Student.objects.prefetch_related('user__applications').all()
students.user__applications # Error
So a student has a foreign key to a User object which is associated with a list of applications. But how do I access the list of applications from the Student object?
Solution
Neither prefetch_related
nor select_related
change the way you access the related data; you do it via the fields or reverse relations, just as you would if you didn't use those methods.
In this case, you have a queryset composed of students; each one of those will have a user
attribute, which gives a User object which in turn has an applications
field which gives another queryset. So, for example:
students[0].user.applications.all()[0]
Answered By - Daniel Roseman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.