开发者

Django template get first?

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='upl开发者_运维技巧oads')

And article.product is a Component.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜