开发者

Output location of images for django-imagekit

I'm trying to output a list of images that belong to each record in my app as below:

pri_photo = vehicle.images.all()[开发者_JAVA技巧:1]
sec_photos = vehicle.images.all()[1:]

This first part is OK. The part I'm having issues with is when I try

pri_photo.original_image.url
sec_photos.original_image.url

The above two lines of code give me a 'QuerySet' object has no attribute 'original_image'. What could be the issue?

I also want the photos in sec_photos to be output as image1, image2,... upto the last one


[:1] just limits the number of records returned by the queryset, still it is a queryset result, not a single object.

to get a single object, you shoud use

[0]

or

[0:1].get()

but it's not safe, as it will raise an error if none objects match the query. to do it properly, use filter() and if result are present then get(), or

try:
    #get()
except modelname.DoesNotExist:
    # do shomething else

also, if you are looking for the latest object, maybe you can just use http://www.djangoproject.com/documentation/models/get_latest/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜