The first Django template?

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 have found very little documentation).

Is there any other 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 this 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 Component .

+4
source share
1 answer

What 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

+11
source

All Articles