views:

198

answers:

1

I need to do something like

{{ article.product.images.first.image.url }}

In my template, but there is no "first" (images is a RelatedManager for which I found very little documentation).

Is there another way to do this? I can get the first model with

{{ article.product.images.get_query_set|first }}

But then I need to dig a few properties deeper, and that doesn't work.


If it helps, my image model looks like this:

class ComponentImage(models.Model):
    component = models.ForeignKey(Component, related_name='images')
    image = models.ImageField(upload_to='uploads')

And article.product is a Component.

+3  A: 

How about {{article.product.images.all.0.image.url}}' ?

This is the same as the python expression article.product.images.all[0].image.url.

Another option is to add a method to the product model that returns images[0].image.url

Tom Leys
Genius! I was thinking about how I could access `[0]` but I wasn't sure of the proper syntax. You actually need a `.all.` in there too, like `{{article.product.images.all.0.image.url}}` to make it work.
Mark