Home > Net >  Fetching data from models using OneToOneField in Django
Fetching data from models using OneToOneField in Django

Time:05-29

well my college is making us go learn a framework and make a website with it in a month, and it's really killing me, because of that I couldn't really get a good understanding of the Django framework as I am making progress while watching YouTube vids and reading docs. Anyways my models are all messed up which made the job even harder, and whenever I solve a problem another one arises, but the deadline is close and making any changes to the models will cost me a great deal of time. This time my problem is about fetching data. The concerned models are the following:

The User class for authentication

class User(AbstractBaseUser, PermissionsMixin):
    id = models.AutoField(primary_key=True,null=False)
    username = models.CharField(max_length=50)
    email = models.EmailField(unique=True)
    nom = models.CharField(max_length=255)
    prenom = models.CharField(max_length=255)
    usertype = models.CharField(choices=types,max_length=20,default="user")
    date_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now=True)
    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    student_data = models.OneToOneField(Etudiant, on_delete=models.CASCADE,blank=True, null=True,related_name='Etudiant_access')
    Prof_data = models.OneToOneField(Prof, on_delete=models.CASCADE,blank=True, null=True)
    objects=UserManager()

    def __str__(self):
        return  self.prenom   " "   self.nom 

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

The Students(Etudiant) class for managing the students:

class Etudiant(models.Model):
    filiere = models.ForeignKey(Filiere, on_delete=models.DO_NOTHING)
    classe = models.ForeignKey(Classe,null=True, on_delete=models.DO_NOTHING)
    notes = models.ManyToManyField(note,blank=True, null=True)

The class Classe (LMAO) for managing the different classes:

class Classe(models.Model):
    #Cla_id = models.AutoField(primary_key=True, null=False)
    Designation = models.CharField(max_length=100)
    filiere = models.ForeignKey(Filiere, on_delete=models.CASCADE)
    Epreuve = models.ManyToManyField(Epreuve,blank=True, null=True)
    def __str__(self):
        return self.Designation

The thing is that I wanna fetch all data of the Users that are students (which means that their Prof_data attribute is blank/null and their student_data attribute is pointing to the Etudiant(Student) class while having an Etudiant.classe attribute equals to a value in the view's parameters

I've solved a great deal of it but I'm stuck at the end This is my view function:

@login_required
def class_info(request,design):
    
#getting the Classe(s) from the url which Designation == design
    classe_now = Classe.objects.get(Designation=design)
    print(classe_now) # This works like a charm
    
#getting the Students objects that are part of the class_now
    Etudiants = Etudiant.objects.filter(classe=classe_now)
    print(Etudiants) # This works too. It returns the 'Etudiant' objects from where i wanna access to the Users data
    
#getting the User data of the student objects (This is where i get confused)
    students_to_show = User.objects.filter(student_data=Etudiants)
    pprint(students_to_show)
    
    return render(request, 'Prof/class_info.html')

I am really confused, you are truly my last hope in this, and thank you for your time.

CodePudding user response:

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.

  • Related