1👍
✅
Assuming your template was given a variable called projects
that is a queryset of all projects, you could do:
{% for project in projects %}
<h1>{{ project.title }}</h1>
<ol>
{% for help in project.helpwanted_set.all %}
<li>{{ help.Position }}</li>
{% endfor %}
</ol>
{% endfor %}
You could also pass in a variable called, say, positions
, that is the queryset of all positions available, and use the regroup
template tag to display them:
{% regroup positions by project as positions_list %}
{% for project in positions_list %}
<h1>{% project.grouper %}</h1>
<ol>
{% for position in project.list %}
<li>{{ position.Position }}</li>
{% endfor %}
</ol>
{% endfor %}
0👍
At first you need to name the foreignkey used in HelpWanted models like that:
project = models.ForeignKey(Project, related_name='jobs')
After that you can, in your view, load your project with all the jobs related to it in one query with:
Project.objects.(your filter).select_related().prefetch_related('jobs')
And in your template use
{{ project }}
<ul>
{% for job in project.jobs.all %}
<li>{{ job }}</li>
{% endfor %}
</ul>
Hope that can help you 😉
👤y0no
Source:stackexchange.com