How to handle nested backward lookups in a template for a generic detail view?

33 views
Skip to first unread message

Thomas Hughes

unread,
Aug 7, 2017, 3:52:15 PM8/7/17
to Django users

I have a generic detail view serving content for 'project' to an HTML template with the following code: 

{% for detail in project.projectdetail_set.all %}
    {% for image in detail.projectdetailimage_set.all %}
        do something with image
    {% endfor %}
{% endfor %}

and my models look like:

class Project(models.Model):
    name = models.CharField(max_length=1000)
    start_date = models.DateField()
    abstract = models.TextField()
    abstract_image = models.ImageField(storage=PROJECT_STORAGE)

    def __str__(self):
        return self.name

class ProjectDetail(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    name = models.CharField(max_length=1000)
    text = models.TextField()

    def __str__(self):
        return self.name

class ProjectDetailImage(models.Model):
    detail = models.ForeignKey(ProjectDetail, on_delete=models.CASCADE)
    image = models.ImageField(storage=PROJECT_STORAGE)

It looks like generic detail view only arranges for backward lookup on the 'project' via .projectdetail_set.all but not on the 'detail' as the HTML for .projectdetailimage_set.all just never shows up in the HTML source. I am wondering then what is the proper way to handle nested ForeignKeys like this, basically like a Book > Section > Chapter structure where a Book has several Sections and a Section has several Chapters and Chapters are only associated with Sections and Sections are only associated with Books.

Thomas Hughes

unread,
Aug 9, 2017, 8:33:13 AM8/9/17
to Django users
Just FYI - the issue was with using 'image.url' rather than 'image.image.url' in code labeled with 'do something with image' in the HTML template above.

"Александр Христюхин (roboslone)"

unread,
Aug 9, 2017, 9:08:54 AM8/9/17
to django...@googlegroups.com
Hi,

First of all, you can use `related_name` in your model fields to get pretty backref names (so you don't have to use image_set everywhere).

Second, you're missing dots in your template (`for detail in project.projectdetail_set.all`).

And finally, your model is called ProjectDetailImage and it has a field, that is named `image`. So you won't be able to get rid of `image.image.url`. However, you can move your image to ProjectDetail model (add field `image = models.ImageField(...)`) and use it like so:

{% for detail in project.projectdetail_set.all %}
    <img src="{{ detail.image.url }}"/>
{% endfor %}

-- 
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/aca8ff0e-cf46-407d-9a58-36feb7d76276%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

"Александр Христюхин (roboslone)"

unread,
Aug 9, 2017, 9:10:33 AM8/9/17
to django...@googlegroups.com
Whoops, looks like all dots are in place, my mistake. First and last points are still valid, though.

Thomas Hughes

unread,
Aug 9, 2017, 11:19:42 AM8/9/17
to Django users
Thank you for your reply.

I understand your third point.  In this application though I have a many images to a single detail structure.

For your first point "use `related_name` in your model fields to get pretty backref names (so you don't have to use image_set everywhere)", how do I do that and maintain the ability to reference the data via the two-fold nested loop I need in my HTML template?

Thanks,

Tom

"Александр Христюхин (roboslone)"

unread,
Aug 9, 2017, 2:28:49 PM8/9/17
to django...@googlegroups.com
You still can reference the data:


class Foo(models.Model):
    pass

class Bar(models.Model):
    foo = models.ForeignKey(Foo, related_name='bars')

f = Foo()
b1 = Bar(foo=f)
b2 = Bar(foo=f)

f.bars.all()  # <QuerySet [b1, b2]>


Reply all
Reply to author
Forward
0 new messages