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.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/cfff20ab-9659-453a-b36c-b7b07f2cc0c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to