Friday, March 17, 2023

Re: sub-list not showing

On Mar 16, 2023, at 2:22 PM, nef <> wrote:

class Student(models.Model):
    #std_matricule = models.CharField(verbose_name='Student matricule', max_length=6, null=False, unique=True, primary_key=True)
    std_matricule = models.CharField(verbose_name='Matricule', unique=True, max_length=16, null=False, blank=False, help_text='Matricule of the student')
    std_parents = models.ForeignKey(Parents, on_delete=models.DO_NOTHING, related_name='Parents', unique=False, null=True, blank=True, verbose_name='Student parents')

I believe that the "related_name" parameter to ForeignKey is the name of the set of THIS model(Student) in the foreign table(Parents). So it should be, "students", not "Parents".

Only then will a parent object refer to the set of students related to it by that name. e.g. parent.students.

This was a point of confusion for me when I read this the first time as well.


No comments:

Post a Comment