Search code examples
pythondjangodjango-oscar

unique values in django template


Hi I am working django oscar and working on a functions which selects values of attribute. I want {{product.examination_filter }} to be list of values without duplicates.

HTML :

<div class="portion">
        {% for product in products%}
                     {{product.examination_filter }}                   
        {% endfor %}                                    
</div>

Models.py

class Product(AbstractProduct):
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct

def examination_filter(self):
    attributes = self.attribute_values.all()
    for attribute in attributes:
        if attribute.attribute.name == 'examination':
            return (attribute.value)

I am getting duplicate values because of for loop on each product. How can I make a unique list of values in django template language ? Note that views.py is handeled by oscar and I am getting products (group of product) on my html from oscar logic. Please help basically i want this values to be displayed as filters for products on webpage.


Solution

  • You would want to do that with a separate query:

    attributes = ProductAttribute.objects.distinct()
    

    Put attributes into your template and you should have a list you can filter on. You may need to make the class ProductAttribute, which inherits from AbstractProductAttribute.