sub-list not showing

80 views
Skip to first unread message

nef

unread,
Mar 16, 2023, 2:22:18 PM3/16/23
to Django users
Hi all,
I am facing problem to display a sub-list.
In my model, I ahave a Student and Parent models. A prent can have one or more students.
I want to list all the student in the parent page, but it is not showing. 
Please see here my code.
Models

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')
    std_email = models.EmailField(verbose_name='Email', null=False, blank=True, help_text='Enter the email of the student or leave blank if not exist')
    std_password = models.CharField(verbose_name='Password', max_length=512, null=False, blank=True, help_text='Type the password with 6 characters minimum')
    std_surname = models.CharField(verbose_name='Surname', null=False, blank=False, max_length=128, help_text='Type the Surname of the student as in the birth certificate')
    std_firstname = models.CharField(verbose_name='First name', null=False, blank=True, max_length=128, help_text='Type the student first name')
    std_midlename = models.CharField(verbose_name='Midle name', null=False, blank=True, max_length=128, help_text='Type the student first name')
    std_nickname = models.CharField(verbose_name='Student Nickname', max_length=64, null=False, blank=True, help_text='If exist, type student nickname here')

lass Parents(models.Model):
    father_surname = models.CharField(verbose_name='Father surname', max_length=128, null=False, blank=True, help_text='Student Father surname as in the birth certificate')
    father_firstName = models.CharField(verbose_name='Father name', max_length=128, null=False, blank=True)
    father_phone = models.CharField(verbose_name='Father phone number', max_length=24, null=False, blank=True, help_text='Phone number of the Father')
    father_dateOfBirth = models.DateField(verbose_name='Father date of birth', null=True, blank=True)
    father_placeOfBirth = models.CharField(verbose_name='Father place of birth', max_length=512, null=True, blank=True)
    father_nationality = models.CharField('Father nationality', max_length=256, null=False, blank=True)
    father_adress = models.CharField(verbose_name='Father resident adress', max_length=512, null=False, blank=True)  
    father_occupation = models.CharField(verbose_name='Father occupation', max_length=512, null=False, blank=True)
    mother_surname = models.CharField(verbose_name='Mother surname', null=False, max_length=128, help_text='Student Father name as in the birth certificate')
    mother_firstName = models.CharField(verbose_name='Mother name', max_length=128, null=False, blank=True)
    mother_phone = models.CharField(verbose_name='Mother phone number', max_length=64, null=False, blank=True, help_text='Phone number of the mother')
    mother_dateOfBirth = models.DateField(verbose_name='Mother date of birth', null=True, blank=True)
    mother_placeOfBirth = models.CharField(verbose_name='Mother place of birth', max_length=512, null=False, blank=True)
    mother_nationality = models.CharField('Mother nationality', max_length=512, null=False, blank=True)
    mother_adress = models.CharField(verbose_name='Mother resident adress', max_length=512, null=False, blank=True)  
    mother_occupation = models.CharField(verbose_name='Mother occupation', max_length=512, null=False, blank=True)
   

View
def parentsDetails(request, pk):
    parentObj = Parents.objects.get(parent_id=pk)
    context = {'parentObj': parentObj}
    return render(request, "students_management_app/parents-single.html", context)

Template
{% extends 'main.html' %}

    {% block content %}
       
        <h1>A parent page for more details </h1>
        <!-- <img src= "{{ buildingObj.buildingIMG1.url }}"> -->
        <h1>{{parentObj.std_matricule}}</h1>
        <br>
        <h2>Father full name: {{parentObj.father_firstName}} {{parentObj.father_surname}}</h2>
        <br>
        <h2>Mother full name: {{parentObj.mother_firstName}} {{parentObj.mother_surname}}</h2>
        <p>
            Register date: {{parentObj.parent_createDate}}
        </p>
        <p><a href="{% url 'parents-list' %}">Add parents</a><br/></p>
        <br/>
           
        {% if parentObj.student_set.all %}
            {% for student in parentObj.students_set.all %}
           
                <h2>List of students</h2>
                <p>{{student.std_matricule}}</p>
                <p>{{student.std_firstname}} {{student.std_midlename}} {{student.std_surname}}</p>
                <p>{{student.std_sex}}</p>

            {% endfor %}
        {% endif %}
        <p>No Student found in the database</p>
       
    {% endblock content %}
       
    <p>Footer</p>

The page is displaying well with all the information for the parent, but not student data.
Thank you
Eric

Namanya Daniel

unread,
Mar 16, 2023, 5:00:13 PM3/16/23
to django...@googlegroups.com
Hello… am using a phone to reply this but I would love to give a hint on something. When you have a child, it’s means there’s a parent foreign key in the child model. You can use grouper to group child model results so that every is grouped together under a particular parent field. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e44e886-b51b-4478-b1b5-dfd5924a5822n%40googlegroups.com.

Namanya Daniel

unread,
Mar 16, 2023, 5:01:53 PM3/16/23
to django...@googlegroups.com
You need to achieve related fields to achieve the most of this… thank you 

nef

unread,
Mar 16, 2023, 11:01:50 PM3/16/23
to Django users
Hi Daniel,
Thanks for your feedback.
Please, is there anyone  who can help me on how to do it?
Thank you

Brian Carey

unread,
Mar 17, 2023, 12:37:30 AM3/17/23
to django...@googlegroups.com

A quick glance: In your temple for loop you have 'student_set' in the conditional and  'students_set' in the loop. That doesn't seem right.

Prosper Lekia

unread,
Mar 17, 2023, 2:42:32 AM3/17/23
to django...@googlegroups.com
The easiest way to do this is to have a query of all students that has that parent.
Add Something like this to your parent detail views,

students = Students.objects.filter(parent__id = pk)
context["students"] = students


Your Template should be

{% for student in students %}
 {{student.std_matricule}}

{% endfor %}


Sandip Bhattacharya

unread,
Mar 17, 2023, 4:02:21 AM3/17/23
to django...@googlegroups.com


On Mar 16, 2023, at 2:22 PM, nef <frnc...@gmail.com> 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.



Thanks,
  Sandip


nef

unread,
Mar 18, 2023, 8:36:50 AM3/18/23
to Django users
Thanks to all of you for your feedback.
I am trying the 2 suggestions, but none of it works.
For the use of the uuid in the view, here is the code:
def parentsDetails(request, pk):
    parentObj = Parents.objects.get(parent_id=pk)
    std = Student.objects.filter(student_id = Parents.parent_id)
    context = {'parentObj': parentObj, 'std': std}
    # context = {'parentObj': parentObj}
    return render(request, "students_management_app/parents-single.html", context)
Here is the template:
       {% if std %}
            {% for student in std %}
           
                <h2>List of students</h2>
                <p>{{student.std_matricule}}</p>
                <p>{{student.std_firstname}} {{student.std_midlename}} {{student.std_surname}}</p>
                <p>{{student.std_sex}}</p>

            {% endfor %}
        {% endif %}
        <p>No Student found in the database</p>
I an having this error
ValidationError at /parentsDetails/321a4f9c-0ffb-4759-9155-b489dd32ad18/['“<django.db.models.query_utils.DeferredAttribute object at 0x0000025FFDF3D600>” is not a valid UUID.']
Please help.
Thanks
 NEF

nef

unread,
Mar 20, 2023, 12:33:23 AM3/20/23
to Django users
I just solve the issue by modifying the view.py like this
def parentsDetails(request, pk):
    parentObj = Parents.objects.get(parent_id=pk)
    std = Student.objects.filter(std_parents = parentObj.parent_id)
    # std = parentObj.student_set.all()
    context = {'parentObj': parentObj, 'std': std}
    # context = {'parentObj': parentObj}
    return render(request, "students_management_app/parents-single.html", context)

Thanks to all for your support

Adekola Aderonmu

unread,
Mar 20, 2023, 2:47:53 AM3/20/23
to django...@googlegroups.com
Hi guys, 
I need your assistance, so I have an external database with a particular  table on it and I want to migrate that table into a new database that I have. Kindly  direct me on how to achieved  this. 
Thank you...

--
Reply all
Reply to author
Forward
0 new messages