production = Production.objects.all() for product in production: try: product.image = product.image_set.get (somefield = somevalue) except Image.DoesNotExist: pass
{% for product in production %} {{ product.title }} <Img src = "{{ product.image.image.name }}" alt = "{{ product.title }}"> {% endfor %} or even better implement the 'get_image' method for the Product. But please note that these solutions are executing SQL queries for each product. It may be better to denormalize the DB and add a FK field to Product model pointing to the main Image model. On 26 дек, 23:56, BigBlogMaker <bigblogma...@gmail.com> wrote: > I'm sorry for my english, but on other groups nobody does not answers. > > In view defined variables: > > production = Production.objects.all () > images = {} > for product in production: > try: > image = product.image_set.get (somefield = somevalue) > images [product.id] = image > except: > pass > > Now the question is: how to access the elements of images in the > template? > > {% for product in production %} > {{ product.title }} > {% if images %} > <Img src = "{{ images.<we must substitute product.id>.name }}" alt = > "{{ images.<we must substitute product.id>.title }}"/> > {% endif %} > {% endfor %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.