Assume the following example:
models.py
class Invoice(models.model): date = models.DateTimeField()
views.py
# example, don't fetch all in production return render(request, 'mytemplate.html', {'invoices': Invoice.objects.all()})
Then your template will look like this:
mytemplate.html
{% for invoice in invoices %} <p>Invoice {{ invoice.id }} ({{ invoice.date }}) {% if invoice.invoicedetail_set %} <ul> {% for detail in invoice.invoicedetail_set %} <li>Detail {{ detail.id }}</li> {% endfor %} </ul> {% endif %} </p> {% endfor %}
There is a very good tutorial for the admin interface in the Django Documentation: Tutorial: Adding Related Objects .
Wtower
source share