Y
Y
Yzurgzd2020-11-12 10:59:55
Django
Yzurgzd, 2020-11-12 10:59:55

How to avoid repeating values?

models

class Characteristic(models.Model):
    name = models.CharField('Наименование', max_length=50)
    slug = models.SlugField(max_length=160, unique=True)

    def get_characteristics_product(self):
        return ProductCharacteristic.objects.filter(characteristic__slug=self.slug)


class ProductCharacteristic(models.Model):
    product = models.ForeignKey(
        Product, verbose_name='Товар', on_delete=models.CASCADE)
    characteristic = models.ForeignKey(
        Characteristic, verbose_name='Характеристика', on_delete=models.CASCADE)
    value = models.CharField('Значение', max_length=50)


views
def get_characteristics(self):
        return Characteristic.objects.distinct()


Output in template
{% for characteristic in view.get_characteristics %}
    <h4>{{ characteristic.name }}</h4>
    {% for product_characteristic in characteristic.get_characteristics_product %}
        {{ product_characteristic.value }}
    {% endfor %}
{% endfor %}


I have two value values ​​displayed, if the values ​​are value. How can I display only one value without repetition?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-11-12
@tumbler

ProductCharacteristic and where is the unique key for a product-feature pair?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question