如何为同一产品添加多个产品变体?

时间:2016-07-10 15:46:01

标签: python django

我制作了服装产品,我希望有两种相同产品:尺寸和颜色。

这是我的models.py

class Product (models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(decimal_places=2,max_digits=1000)
    brand = models.CharField(max_length=300)
    active =models.BooleanField(default=True)
    weight = models.CharField(max_length=100, null=True, blank=True)
    size = models.CharField(max_length=120)

#For variation   
class Variation (models.Model):
    product = models.ForeignKey(Product)
    title = models.CharField(max_length=120)
    price = models.DecimalField(decimal_places=2, max_digits=20)
    size = models.CharField(max_length=120, choices=CHOOSE_SIZE)

    def __str__(self):
        return self.title

到目前为止,我只能添加一个变体,就像我在产品上添加白衬衫一样,我为衬衫添加了颜色变化,现在我该如何添加颜色变化。

这是我的HTML代码

{% extends "base.html" %}
{% block content %} {{ object.title }} {#{{ object.variation_set.all }}#} 
<select class="form-control"> 
{% for vari_obj in object.variation_set.all %} 
    <option value="{{ vari_obj.id }}">{{ vari_obj }} </option> 
{% endfor %} 
</select> 
{% endblock %} 

1 个答案:

答案 0 :(得分:0)

class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(decimal_places=2,max_digits=1000)
    brand = models.CharField(max_length=300)
    active =models.BooleanField(default=True)
    weight = models.CharField(max_length=100, null=True, blank=True)

class Variations(models.Model):
    product = models.ForeignKey(Product)
    title = models.CharField(max_length=120)
    price = models.DecimalField(decimal_places=2, max_digits=20)
    size = models.CharField(max_length=120, choices=CHOOSE_SIZE)
    color = models.CharField()

    def __str__(self):
        return self.title

从产品型号中删除尺寸。在Variations模型中放置尺寸和颜色。