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]>


> On 9 Aug 2017, at 18:19, Thomas Hughes <ezero.technol...@gmail.com> wrote:
> 
> 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
> 
> On Wednesday, August 9, 2017 at 9:10:33 AM UTC-4, Александр Христюхин wrote:
> Whoops, looks like all dots are in place, my mistake. First and last points 
> are still valid, though.
> 
>> On 9 Aug 2017, at 16:08, Александр Христюхин (roboslone) <robo...@gmail.com 
>> <javascript:>> wrote:
>> 
>> 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 %}
>> 
>>> On 9 Aug 2017, at 15:33, Thomas Hughes <ezero.te...@gmail.com 
>>> <javascript:>> wrote:
>>> 
>>> 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.
>>> 
>>> On Monday, August 7, 2017 at 3:52:15 PM UTC-4, Thomas Hughes wrote:
>>> 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 <http://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 <http://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...@googlegroups.com <javascript:>.
>>> To post to this group, send email to django...@googlegroups.com 
>>> <javascript:>.
>>> Visit this group at https://groups.google.com/group/django-users 
>>> <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
>>>  
>>> <https://groups.google.com/d/msgid/django-users/aca8ff0e-cf46-407d-9a58-36feb7d76276%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>> For more options, visit https://groups.google.com/d/optout 
>>> <https://groups.google.com/d/optout>.
>> 
> 
> 
> -- 
> 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 
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com 
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users 
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/ae732c74-1ab9-4176-9a6f-8fbebff37fc8%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/ae732c74-1ab9-4176-9a6f-8fbebff37fc8%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
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/CC608C69-50CD-44F4-BDC3-2B37AB0DEBF2%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to