[Answer]-Django queryet to print product and it's models

1👍

Its the same in the template:

<ul>
{% for product in objects %}
    <li>{{ product }}
        <ul>
    {% for product_model in product.productmodel_set.all %}
       <li>{{ product_model }}</li>
    {% endfor %}
    </ul></li>
{% endfor %}
</ul>

Use the following view:

def product_list(request):
   return render(request, 'template.html', {'objects': Product.objects.all()})

Or, if you prefer, use the generic views:

class ProductList(ListView):
    template = 'template.html'
    queryset = Product.objects.all()

Leave a comment