Use select_related or just reference it in the template as an attribute.
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#select-related
Product.objects.select_related(‘photo’)
{% if product.photo %}
…
{% endif %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/c8c00d69-f136-4c65-a87b-8466bcb2d866%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ecf48599ce9d44629e2d0bd68dfbfa34%40iss2.ISS.LOCAL.
Then use the reverse accessor:
https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
Product.objects.select_related(‘photo_set’)
{% for photo in product.photo_set.all %}
{{ photo }}
{% empty %}
This product has no photos.
{% endfor %}
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABCRpYOKy7ZaC0xh%3D8m6AtoeLx0fCvo3irPhPh8SK7YZJ5YC6Q%40mail.gmail.com.