Associating two database tables which have ForeignKey relation in models.py in django templates
-
06-07-2019 - |
Question
I have problem about django template-db query relationship.
For example, I created two tables in db (e.g. Menu & Submenu). Submenu, as you can guess, has a ForeignKey relationship with Menu.
On the template side how can Ido db-query according to this relationship. I want to have a link for Menu such as:
{% for menu in menu_list %}
<li>menu.title</li>
# And here put each Submenu which has ForeignKey relation with specific Menu
{% endfor %}
Thanks in advance...
Solution
Something like {% for submenu in menu.submenu_set.all %}{# do something #}{% endfor %}
.
You should really read the relevant docs before asking a question like this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow