我正在django探索模型,在那里我试图为电子商务产品创建一个模型。我设计的模式目前正在遵循。
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=80)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
total_stock = models.PositiveIntegerField(default=0)
def __str__(self):
return self.name
class Attribute(models.Model):
'''
attribute can be like color, material, size and many more
'''
name = models.CharField(max_length=80)
def __str__(self):
return self.name
class AttributeValue(models.Model):
'''
Values for the selected attribute like for size attr
the values can be Large, Medium, Small and etc
'''
name = models.CharField(max_length=100)
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
price = models.DecimalField(decimal_places=2, max_digits=10)
discount = models.DecimalField(decimal_places=2, max_digits=10)
stock = models.PositiveIntegerField(default=0)
def __str__(self):
return self.name
class ProductAttribute(models.Model):
'''
Associate Particular attribute to Particular product
'''
product = models.ForeignKey(Product, on_delete=models.CASCADE)
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
def __str__(self):
return self.product.name
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to = 'pic_folder/')
def __str__(self):
return self.product.name我的问题是,当我研究可伸缩的电子商务产品设计(从更好的表关系和涵盖电子商务中的大部分因素的角度来看)时,我看到了各种各样的表,比如ProductVariant、ProductVariantImage、ProductOptions等等,所以我对这些术语感到困惑。有人能帮助我用例子让我明白吗?我如何调整我的models.py中的表?
这是链接
发布于 2019-09-29 01:51:53
我想你只是想了解这些术语和它们之间的关系,对吧?一旦您理解了,您就可以决定如何调整模式和模型。
ProductVariant:A产品的“版本”。从电子商务的角度来看,这可能意味着不太适合属性或模型。例如,一个产品可以有一个变体:
我认为您可以不使用ProductVariant模型,只需使用属性就可以工作了。使用ProductVariant作为对预先存在的Product (Product.id上的外键约束)的引用可能是有意义的。见这里和这里。
ProductVariantImage:ProductImage.的一个版本
ProductOptions:产品的选项。您只需使用属性即可。这个表/模型似乎与属性&并没有什么不同。
https://stackoverflow.com/questions/58151447
复制相似问题