如何将一对多关系传递给objects.all()查找为可迭代的上下文

时间:2022-09-01 21:25:58

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.

我在试图绕过这个问题时遇到了麻烦。多对一关系已有详细记录,但我找不到关于如何将model.objects.all()查找的所有关系传递给上下文的示例。

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.

我尝试过imagesets = ImageSet.objects.all()。prefetch_related()但它似乎只适用于ManyToMany关系。

2 个解决方案

#1


1  

Given an instance of the ImageSet model, you can access the related images with...

给定ImageSet模型的一个实例,您可以使用...访问相关图像

images = my_imageset.image_set.all()

...so you can just change the template to...

...所以你可以把模板改成......

{% for imageset in imagesets %}
    {{ imageset.title }}
    {% for image in imageset.image_set.all  %}
        <img src="{{image.image_file.url}}" alt="">
    {% endfor %}
{% endfor %}

See the Django documentation on Related objects for more information.

有关更多信息,请参阅有关Related对象的Django文档。

#2


0  

views.py will be like this:-

from django.shortcuts import render
from models import Image, ImageSet

def images(request):
    t=get_template("template.html")
    imagesets = ImageSet.objects.all()
    image = Image.objects.all()
    html=t.render(Context({'imagesets':imagesets,'image':image}))
    return HttpResponse(html)

In template.html:

{% for image in imageset.images  %} line will not work as imageset.image does not
exist because Imageset model having only one field title so you cat't access that.
if you   want to access image field of Image model then from views.py you have to send
that object also.
and <img src="{{image.url}}" alt=""> will not work. where is your url field??

---------thanks-------

#1


1  

Given an instance of the ImageSet model, you can access the related images with...

给定ImageSet模型的一个实例,您可以使用...访问相关图像

images = my_imageset.image_set.all()

...so you can just change the template to...

...所以你可以把模板改成......

{% for imageset in imagesets %}
    {{ imageset.title }}
    {% for image in imageset.image_set.all  %}
        <img src="{{image.image_file.url}}" alt="">
    {% endfor %}
{% endfor %}

See the Django documentation on Related objects for more information.

有关更多信息,请参阅有关Related对象的Django文档。

#2


0  

views.py will be like this:-

from django.shortcuts import render
from models import Image, ImageSet

def images(request):
    t=get_template("template.html")
    imagesets = ImageSet.objects.all()
    image = Image.objects.all()
    html=t.render(Context({'imagesets':imagesets,'image':image}))
    return HttpResponse(html)

In template.html:

{% for image in imageset.images  %} line will not work as imageset.image does not
exist because Imageset model having only one field title so you cat't access that.
if you   want to access image field of Image model then from views.py you have to send
that object also.
and <img src="{{image.url}}" alt=""> will not work. where is your url field??

---------thanks-------