选择每个相册中的所有照片 (Django)

发布于 2024-09-07 20:49:47 字数 1083 浏览 3 评论 0原文

我需要一些帮助(抱歉我的英语不好)。

我正在尝试获取每个相册中的所有照片。我不知道如何进行查询。 我需要这些数据(并按属性“顺序”对照片进行排序,这会很棒)。

House1-title

photo1: descritpion photo2: descritpion

photo3

: descritpion

House2-title

photo1: descritpion

photo2: descritpion

photo3: descritpion

模型(略)是这样的,比较多方便查询吗?哪个更有效率? (我知道,最好的解决方案是使用内存缓存)。

选项A:

class House(ImageModel):
      title = models.CharField(max_length=25)
      photos = models.ManyToManyField('Photo')


class Photo(ImageModel):
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

选项B:

class House(ImageModel):
      title = models.CharField(max_length=25)

class Photo(ImageModel):
    house = models.ForeignKey(House)
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

谢谢!

I need some help (sorry for my poor english).

I 'm trying to get all photos from each album. I don't know how to make the query.
I need this data (and order the photos by atribute "order" it will fantastic).

House1-title

photo1: descritpion

photo2: descritpion

photo3: descritpion

House2-title

photo1: descritpion

photo2: descritpion

photo3: descritpion

The model (abbreviated) looks like this, which is more easy to make the query? and wich is more efficient? (i know, the best solution is using memcache).

Option A:

class House(ImageModel):
      title = models.CharField(max_length=25)
      photos = models.ManyToManyField('Photo')


class Photo(ImageModel):
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

Option B:

class House(ImageModel):
      title = models.CharField(max_length=25)

class Photo(ImageModel):
    house = models.ForeignKey(House)
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-09-14 20:49:47

选项 B. 是正确的方法。

House.objects.all().select_related()


{% for house in house_qs %}
     {{house.name}}

    {% for photo in house.photo_set.all %}
       {{photo}}
    {% endfor %}

{% endfor %}

option B. is the right way.

House.objects.all().select_related()


{% for house in house_qs %}
     {{house.name}}

    {% for photo in house.photo_set.all %}
       {{photo}}
    {% endfor %}

{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文