如何以正确的顺序链接查询集,而不是按日期排序,而是按外键排序?

发布于 2024-11-17 14:21:53 字数 1668 浏览 1 评论 0原文

我正在建立一个照片库,而发布的照片​​库并没有给我我需要的东西,所以我从头开始构建它。

在索引页面中,我需要从相册模型中获取随机相册(这很好),并为每个相册从照片模型中获取其中一张照片(随机)。

模型:

class Album(models.Model):
    title = models.CharField('כותרת', max_length=100, db_index=True)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    creator = models.ForeignKey(User, related_name='galleries_creator', verbose_name='נכתב ע"י')

class Photo(models.Model):
    title = models.CharField('כותרת', max_length=100)
    album = models.ForeignKey(Album, verbose_name='שייך לאלבום')
    photo = models.ImageField('תמונה', blank=True, upload_to=get_image_path)
    photo_mid = models.ImageField('תמונה בינונית', blank=True, upload_to='images/galleries/mid/', editable=False)
    photo_thumb = models.ImageField('תמונה קטנה', blank=True, upload_to='images/galleries/thumbs/', editable=False)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    is_landscape = models.NullBooleanField(blank=True, verbose_name='האם תמונת לנדסקייפ', editable=False)

正如我所见,事情是这样的,为了迭代模板中的相册并获取相册的正确照片,我需要链接相册列表和我得到的照片列表,但是在正确的顺序(相册对象、照片对象等),但我得到的是所有相册,然后是所有照片(按正确的顺序,但在模板中没有任何效果,很明显)。

观点:

def index(request):
    albums = Album.objects.all().order_by('?')[:10]
    album_photo_lst = []
    for album in albums:
        album_photo_lst.append(Photo.objects.filter(album=album).order_by('?')[:1])
    album_list = list(chain(albums,album_photo_lst))
    return render_to_response('galleries/index.html',{'albums':album_list},  context_instance=RequestContext(request))

也许我把这个问题过于复杂化了,也许有人可以帮助我解决这个问题。

10 倍, 埃雷兹

I am building a photo gallery, and the ones that are published don't give me what i need so i am building it form scratch.

In the index page i need to get random albums (this is fine) from the Album model and for each album to bring one of its photos (random) from the Photo model.

Models:

class Album(models.Model):
    title = models.CharField('כותרת', max_length=100, db_index=True)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    creator = models.ForeignKey(User, related_name='galleries_creator', verbose_name='נכתב ע"י')

class Photo(models.Model):
    title = models.CharField('כותרת', max_length=100)
    album = models.ForeignKey(Album, verbose_name='שייך לאלבום')
    photo = models.ImageField('תמונה', blank=True, upload_to=get_image_path)
    photo_mid = models.ImageField('תמונה בינונית', blank=True, upload_to='images/galleries/mid/', editable=False)
    photo_thumb = models.ImageField('תמונה קטנה', blank=True, upload_to='images/galleries/thumbs/', editable=False)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    is_landscape = models.NullBooleanField(blank=True, verbose_name='האם תמונת לנדסקייפ', editable=False)

The thing is, as i see it, that in order to iterate over the albums in the template and get the correct photo for the album, i need to chain the list of albums and the list of photos i got, but in the right order (album object, photo object etc), but what i am getting is all the albums and then all the photos (in the right order, but in the template nothing is working, obvious).

View:

def index(request):
    albums = Album.objects.all().order_by('?')[:10]
    album_photo_lst = []
    for album in albums:
        album_photo_lst.append(Photo.objects.filter(album=album).order_by('?')[:1])
    album_list = list(chain(albums,album_photo_lst))
    return render_to_response('galleries/index.html',{'albums':album_list},  context_instance=RequestContext(request))

Maybe i am over complicating this, and maybe some one can help me get thru this.

10x,
Erez

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

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

发布评论

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

评论(1

傲世九天 2024-11-24 14:21:53

在不尝试更改您的查询的情况下,我建议使用(相册,照片) 元组 而不是长列表:

for album in albums:
    album_list.append((album, Photo.objects.filter(album=album).order_by('?')[0]))

现在使用

    {{ for album, photo in album_list }}

(并且一定要检查如果相册中有 0 张照片会发生什么。)

Without tryiung to change your queries, I suggest to use a list of (album, photo) tuples instead of a long list:

for album in albums:
    album_list.append((album, Photo.objects.filter(album=album).order_by('?')[0]))

now use

    {{ for album, photo in album_list }}

(And be sure to check what happens if there are 0 photos in the album.)

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