I'm having trouble trying to wrap my head around this. many-to-one relationships are well documented, but I can't find an example on how to pass all of the relationships of a model.objects.all() lookup to context.
I'm trying to do something like
models.py
from django.db import models
class Image(models.Model):
image_file = models.ImageField()
imageset = models.ForeignKey(ImageSet)
class ImageSet(models.Model):
title = models.CharField(max_length)
views.py
from django.shortcuts import render
from models import Image, ImageSet
def images(request):
imagesets = ImageSet.objects.all()
return render(request, 'template.html', {'imagesets': imagesets})
template.html
{% for imageset in imagesets %}
{{ imageset.title }}
{% for image in imageset.images %}
<img src="{{image.image_file.url}}" alt="">
{% endfor %}
{% endfor %}
I've tried imagesets = ImageSet.objects.all().prefetch_related()
but it only seems to work for ManyToMany relationships.