25👍
✅
Starting from Django 1.4 prefetch_related
does what you want.
Parent.objects.prefetch_related('child_set')
Related(!) django docs : https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related.
7👍
In this case, I think the best thing to do is list the children, then get the parent from them, like this:
children = Child.objects.filter(...).select_related('parent').order_by('parent')
Then in the template, possibly use a regroup
(note the order_by
above):
{% regroup children by parent as parents %}
<ul>
{% for parent in parents %}
<li>{{ parent.grouper }}
<ul>
{% for child in parents.list %}
...
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
- [Django]-Serving large files ( with high loads ) in Django
- [Django]-Django: Admin: changing the widget of the field in Admin
- [Django]-Django: using ModelForm to edit existing database entry
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
- [Django]-Cron and virtualenv
- [Django]-How to receive POST data in django
Source:stackexchange.com