How to retrieve data from deep nested object?

53 views
Skip to first unread message

Said Akhmedbayev

unread,
Apr 26, 2016, 8:33:00 AM4/26/16
to Django users
I am trying to figure out how to loop over deep nested object in Django template.

Here is my app's code

models.py

from django.db import models


class Unit(models.Model):
    unit_number = models.PositiveIntegerField(blank=True, null=True, default=1)
    title = models.CharField(max_length=150, blank=True, null=True)
    scripture_ref = models.CharField(max_length=250, blank=True, null=True)

    def __str__(self):
        return self.title


class Subsection(models.Model):
    title = models.CharField(max_length=150, blank=True, null=True)
    subsection_text = models.TextField(default='SUBSECTION TEXT')
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE)


class Question(models.Model):
    question_text = models.CharField(max_length=2000, default='QUESTION TEXT')
    subsection = models.ForeignKey(Subsection, on_delete=models.CASCADE)

views.py

from django.shortcuts import get_object_or_404
from django.shortcuts import render

from .models import Unit


def unitdetail(request, unit_id):
    unit = get_object_or_404(Unit, pk=unit_id)
    return render(request, 'correspondence_course/unit_detail.html', {'unit': unit,})

unit_detail.html (template)

<h1>Урок {{ unit.unit_number }}</h1>
<h1>{{ unit.title }}</h1>


{% for subsection in unit.subsection_set.all %}
    <h2>{{ subsection.title }}</h2>
    <div>{{ subsection.subsection_text }}</div>
    {% for question in subsection. %} # this is just for reference. It does not work
        <p>{{ question.question_text }}</p>
    {% endfor %}
{% endfor %}

As you can see I have three class models: Unit, Subsection with back relationship with Unit and then Question with back relationship with Subsection.

I managed to show in the template (see above) unit.title alone with subsection.title and subsection.subsection_text. It is all nice, but I cannot figure out how to loop over and display questions that are nested to Subsection(s).

Can you please help me to figure out how make it work?





Vijay Khemlani

unread,
Apr 26, 2016, 9:49:39 AM4/26/16
to django...@googlegroups.com
I guess it is 

{% for question in subsection.question_set.all %}

based on your models

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f0d18f4-57c1-4852-b510-114a9b7fffc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Tagg

unread,
Apr 26, 2016, 9:49:41 AM4/26/16
to django...@googlegroups.com
Does {% for question in subsection.subsection_set.all %} work?

Dan

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f0d18f4-57c1-4852-b510-114a9b7fffc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: Sir Robert Peel House, 178 Bishopsgate, London, United Kingdom, EC2M 4NJ, Company no: 05766374

Said Akhmedbayev

unread,
Apr 26, 2016, 9:56:44 AM4/26/16
to Django users
Unfortunately, it does not work for me :-(

Said Akhmedbayev

unread,
Apr 26, 2016, 9:57:11 AM4/26/16
to Django users
This does not work for me either :-(

Said Akhmedbayev

unread,
Apr 26, 2016, 10:06:43 AM4/26/16
to Django users
Sorry guys this worked for me

{% for question in subsection.question_set.all %}
        <p>{{ question.question_text }}</p>
    {% endfor %}

My problem was that I did not realize that Django is smart enough for not to show default text in the Question body that I placed in the question model. When I inserted a custom text (typed in), everything showed up.

Thanks


On Tuesday, April 26, 2016 at 7:49:39 PM UTC+6, Vijay Khemlani wrote:
Reply all
Reply to author
Forward
0 new messages