In your template you should be able to do something like:
{% for smallbox in bigbox.smallboxes.all %}
{{ smallbox.some_attribute }}<br>
{% endfor %}
Similar question here:
https://stackoverflow.com/a/40569127/4872140
Note that with the many to many, you don't use _set
on smallboxes as you would for a normal foreign key relationship.
INCORRECT: (don't do this)
{% for smallbox in bigbox.smallboxes_set.all %}
{{ smallbox.some_attribute }}<br>
{% endfor %}
There is some discussion on how to use/why the .all method call works in the Django Template Language documentation. More specifically, on the ManyToMany handling.
EDIT:
After reading your comment, I see you have a slightly different issue than I originally read. I think you may need to add another loop inside of the outer loop. It is a little tough here due to the missing details in the models in the post. I'm going to suggest you rename Big_box_small_box_items
to BigBoxSmallBoxItems
, and rename the field from Item
to item
. Review the coding style and stick with it to save yourself some headaches later (https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#model-style)
{% for smallbox in bigbox.smallboxes.all %}
{% for sbitem in smallbox.bigboxsmallboxitems_set.all %}
{{ sbitem.item.your_item_attribute }}<br>
{% endfor %}
{% endfor %}
I doubt this will optimize your queries, and you may end up keeping what you've got. Depending on what you've already got in your view you may wish to review the prefetch_related
tool (https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related)