[Answered ]-Fetching data from models using OneToOneField in Django

1👍

You can filter your User model by selecting all users that do have empty/null relation to Prof model and nonempty/null relation to Etudiant model.

student_users = User.objects.filter(Prof_data__isnull=True, student_data__isnull=False)

then for each stident_user, you can fetch its student data in the following manner:

student_user = student_users[0]
student_user.student_data.filiere
student_user.student_data.classe
student_user.student_data.notes

You can then pass the queryset result to the render function as a context variable. Check this brief tutorial on how to pass data to templates.

Leave a comment