如何以正确的顺序链接查询集,而不是按日期排序,而是按外键排序?
我正在建立一个照片库,而发布的照片库并没有给我我需要的东西,所以我从头开始构建它。
在索引页面中,我需要从相册模型中获取随机相册(这很好),并为每个相册从照片模型中获取其中一张照片(随机
)。
模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不尝试更改您的查询的情况下,我建议使用
(相册,照片)
元组 而不是长列表:现在使用
(并且一定要检查如果相册中有 0 张照片会发生什么。)
Without tryiung to change your queries, I suggest to use a list of
(album, photo)
tuples instead of a long list:now use
(And be sure to check what happens if there are 0 photos in the album.)