[Fixed]-How to iterate into a list on django relation one-to-many

1👍

Your model looks correct to me. I see a couple of potential problems.

  1. Depending on your templating engine, you might have to drop the parenthesis. I think Jinja2 uses them but Django Template Language does not. I think, based on the error message, you’re using the latter (DTL).

  2. You probably just need to call the all method on produto.categoria_set. I’m pretty sure categoria_set is a kind of manager object. You need a QuerySet, which will be returned when you call all.

Here’s what you should try:

{% for categoria in produto.categoria_set.all %}
# SHOW ALL HERE. HOW?
{% endfor %}

Note: this answer is currently in flux because the original question contradicts itself.

Leave a comment