views:

57

answers:

1

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

pri_photo = vehicle.images.all()[: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  A: 

[: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/

zalew
I've managed to get pri_photo working as desired...all that remains is sec_photos
Stephen