A
A
Alexey Belov2018-06-01 14:52:15
Django
Alexey Belov, 2018-06-01 14:52:15

Distinct tag for django?

{% for order_product in order.products.all %}
        <button class="btn">  {{ order_product.product.type }}</button>
 {% endfor %}

How to write a tag for a template that will remove non-unique values, for example, if two potatoes and one pepper come across, then we get onions and peppers at the output, the db option is not suitable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-06-01
@sergey-gornostaev

@register.filter()
def distinct(items):
    return set(items)

{% for order_product in order.products.all|distinct %}
    ...
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question