1👍
As you mentioned here, you are looking for Student data for currently logged in Parent. Hence you can look for Student data directly from Parent object. Like this:
stud_object = request.user.parent.student
This relation works because Parent has a OneToOne relation with CustomUser (I assume Authentication’s custom User model), hence you should get Parent object from request.user.parent
(reverse relation for OneToOne). Also, student field is a ForeignKey of Parent model.
Addionally, I think the relation between Parent and Student should be ManyToMany, because a Student can have multiple parents and one parent can have multiple students (or children).
0👍
There are two possibilities:
- The View code that you have attached should be returning
stud_data
not None, but I am assuming that you know this and this current state of the code is just for debugging purposes. - The
request.user.id
contains a value that doesn’t belong to any student’s ID in the database. As you are usingfilter
, it’s not going to complain about it and just return you an emptyQuerySet
. I’d suggest using theget()
filter here which raises theDoesNotExist
exception and would help in debugging as well.
def home(request):
stud_data = Parents.objects.get(student__id = request.user.id)
return stud_data
Hope it helps!
Best of luck with your new journey!
👤mah
- [Answered ]-'+' in django form
- [Answered ]-ImproperlyConfigured url
- [Answered ]-Filter ForeignKeyFeild Choices in field by value in other field
Source:stackexchange.com