Home > other >  Django - get objects from one table who belong to another objects in other table
Django - get objects from one table who belong to another objects in other table

Time:06-02

I have a project to do which consists to creating a note manager in Django. So I created my tables in sql with foreign keys. And I have been facing a problem for several days. I have a page that lists all the students in the database, and I would like by clicking on a link, to be able to display all the notes belonging to each student.

Here's my SQL tables (étudiants = students / Notes = grades) :

sql students table / sql grade table

models.py :

class Etudiants(models.Model):
    numeroetudiant = models.BigIntegerField(db_column='numeroEtudiant', blank=True, null=True)  # Field name made lowercase.
    nom = models.CharField(max_length=255, blank=True, null=True)
    prenom = models.CharField(max_length=255, blank=True, null=True)
    groupe = models.BigIntegerField(blank=True, null=True)
    photo = models.TextField(blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)
    
    class Meta:
        managed = False
        db_table = 'etudiants'

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


class Notes(models.Model):
    examens = models.ForeignKey(Examens, models.DO_NOTHING, db_column='examens', blank=True, null=True)
    etudiant = models.ForeignKey(Etudiants, models.DO_NOTHING, db_column='etudiant', blank=True, null=True)
    note = models.BigIntegerField(blank=True, null=True)
    appreciation = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'notes'
    
    def __str__(self):
        return "Note de "   self.etudiant.nom   " "   self.etudiant.prenom   " à l'examen de "   self.examens.titre

views.py :

def etudiants(request):
    etudiants = Etudiants.objects.all()
    return render(request, 'etudiants.html', {'etudiants': etudiants, 'notes': notes})


def affichenote(request, id):
    notes = Notes.objects.all()
    return render(request, 'affichenote.html', {'notes': notes})

urls.py :

   path('etudiants/', views.etudiants, name='etudiants'),
   path('affichenote/<int:id>/', views.affichenote, name='affiche-note'),

etudiants.html :

{% extends 'base.html' %} {% block content %}

<div >
  {% for Etudiants in etudiants %}
  <div >
    <div >
      <img src="..."  alt="..." />
      <div >
        <h5 >{{ Etudiants.nom }} {{ Etudiants.prenom }} </h5>
        <p >
          E-mail : {{ Etudiants.email }} <br>
          Numéro étudiant : {{ Etudiants.numeroetudiant }} <br>
          Groupe : {{ Etudiants.groupe }} <br>
        </p> 
        <a href="../affichenote/{{Etudiants.id}}/" >Voir les notes</a>
      </div>
    </div>
  </div>
  {% endfor %}
</div>

{% endblock %}

CodePudding user response:

If you want by clicking on a link, you would be able to display all the notes belonging to each student. So you can apply filter() through student id in the following way:

views.py


def etudiants(request):
    etudiants = Etudiants.objects.all()
    return render(request, 'etudiants.html', {'etudiants': etudiants})
  
def affichenote(request, id):
    notes = Notes.objects.filter(etudiant__id=id)
    return render(request, 'affichenote.html', {'notes': notes})

urls.py

urlpatterns = [
    path('etudiants/', views.etudiants, name='etudiants'),
    path('affichenote/<int:id>/', views.affichenote, name='affichenote'),

]

etudiants.html

<div >
  {% for etudiant in etudiants %}
  <div >
    <div >
      <img src="..."  alt="..." />
      <div >
        <h5 >{{ etudiant.nom }} {{ etudiant.prenom }} </h5>
        <p >
          E-mail : {{ etudiant.email }} <br>
          Numéro étudiant : {{ etudiant.numeroetudiant }} <br>
          Groupe : {{ etudiant.groupe }} <br>
        </p> 
       
        <a href="{% url 'affichenote' etudiant.id %}" >Voir les notes</a>

      </div>
    </div>
  </div>
  {% endfor %}
</div>
    

Then, in affichenote.html, you can access all notes, relating to a student.

affichenote.html

<body>
{% for note in notes  %}
    <p>{{note.edudiant}}</p>
    <p>{{note.note}}</p>
    <p>{{note.appreciation}}</p>

{% endfor %}
</body>

Note: Models in django generally written in singular form, it will be better if you name it Etudiant and Note instead of Etudiants and Notes respectively, as s is already itself added as the suffix for every model.

  • Related