1👍
You don’t really need to include those elements in your QuerySet. In fact, you already have them : you just need to retrieve related objects.
With your code, latest and rows are querysets on Report model which have a foreign key on Part model
# in any *.py files, such as views.py
for report in rows:
# You can access the Part object, so you can access Product,
# so you can access Manufacturer, just by hooking through the reverse relationship
product_name = report.part.product.name
manufacturer_name = report.part.product.manufacturer.name
You can access those element from your templates too :
# in your template.html
{% for report in rows %}
<p>Part: <span>{{ report.part }}</span></p>
<p>Product: <span>{{ report.part.product.name }}</span></p>
<p>Manufacturer: <span>{{ report.part.product.manufacturer.name }}</span></p>
So as you can see, everything is already ship with your queryset.
0👍
You can get the product and manufacturer name for a report by using report.part.product.name
and report.part.product.manufacturer.name
.
So you could do (using your current query):
for report in rows:
product_name = report.part.product.name
manufacturer_name = report.part.product.manufacturer.name
There are better ways to do it, but it depends on what you want to do with the names so you’ll need to be a bit more specific if you want help there. If you do end up using a simple for loop like that you probably want to have a look at select_related to avoid firing off extra queries for every report in rows
.
- [Answer]-How do I do display content in Django based on URL and all recurisive urls
- [Answer]-Search user by username and redirect to the user page in Django
- [Answer]-Closing a Django FieldFile
- [Answer]-Validate nested form in Django