Answer the question
In order to leave comments, you need to log in
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)
def get_characteristics(self):
return Characteristic.objects.distinct()
{% for characteristic in view.get_characteristics %}
<h4>{{ characteristic.name }}</h4>
{% for product_characteristic in characteristic.get_characteristics_product %}
{{ product_characteristic.value }}
{% endfor %}
{% endfor %}
Answer the question
In order to leave comments, you need to log in
ProductCharacteristic and where is the unique key for a product-feature pair?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question