Thursday, March 16, 2023

Re: sub-list not showing

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. 

On Thu, 16 Mar 2023 at 21:22, nef <frncseric@gmail.com> wrote:
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

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP4VW2Uhvx6v49UTnm_hzrzj8A%2B7G%2BdZTWt89i5xOdxFV1iLBw%40mail.gmail.com.

No comments:

Post a Comment