Как проверить цвет и размер вариантов товара в шаблоне Django

Это дублирование вариантов цвета в соответствии с вариантами, мне нужно, чтобы он был только один, если он существует.

Django Admin

Модели:

class Variants(TimeStampedModel):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    size = models.ForeignKey(Size, on_delete=models.CASCADE, blank=True, null=True)
    color = models.ForeignKey(Color, on_delete=models.CASCADE, blank=True, null=True)
    quantity = models.IntegerField(default=0)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.product.name

Просмотров:

class ProductDetailView(DetailView):
    queryset = Product.available.all()  

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["variation_color"] = Variants.objects.filter(color__id=1)
        context["variation_size"] = Variants.objects.filter(size__id=1)
        return context

Шаблон:

{% if product.variants_set.all %}
                    
                    {% if variation_color %}
                    <div class="section">
                        <h6 class="title-attr" style="margin-top:15px;" ><small>Color</small></h6>                    
                        <div>
                            {% for color in product.variants_set.all %}
                            <div class="attr" style="width:25px;background:{{ color.color.code }};"></div>
                            {% endfor %}
                        </div>
                    </div>
                    {% endif %}
                    
            
                    {% if variation_size %}
                    <div class="section">
                        <h6 class="title-attr"><small>Size</small></h6>                    
                        <div>
                            {% for size in product.variants_set.all %}
                            <div class="attr2">{{ size.size }}</div>
                            {% endfor %} 
                        </div>
                    </div>
                    {% endif %}
   
{% endif %}   

Если у кого-нибудь есть решение по этому вопросу, спасибо!

Вернуться на верх