Django manytomany in template format

9.1k Views Asked by At

In one of my models I have a categories field that is a manytomany configuration.

I was wondering how to get the following output in the template.

Category 1, Category 2, Category 3 and Category 4

So basically separate each category with a comma, apart from the last one that then gets replaced with a "and"

1

There are 1 best solutions below

0
On BEST ANSWER
{% for category in myob.categories.all %}
  {{ category }}
  {% if not forloop.last %},{% else %} and {% endif %}
{% endfor %}