【问题标题】:Django model - how to go about this conceptDjango 模型 - 如何处理这个概念
【发布时间】:2018-01-30 16:52:51
【问题描述】:

我的产品型号如下:

class Product(models.Model):
    name = models.CharField(max_length=255)
    handle = models.CharField(max_length=55)
    summary = models.CharField(max_length=255, blank=True)
    category = models.ForeignKey(Category)

以上只是产品详情,但有 3 种定价类型:

  1. 标准 - 通过填写价格字段进行正常正常定价
  2. 变体 - 根据产品变体(尺寸、颜色等)及其各自的价格定价
  3. 组合 - 这是其他已保存产品的组合,由用户提供自定义价格。

对于 1 和 2,我有以下型号。如果产品型号在 StandardProduct 型号上的价格超过 1,那么我知道它有变体。

class StandardProduct(models.Model):
    variant_type = models.CharField(max_length=55)
    variant_name = models.CharField(max_lenght=55)
    product = models.ForeignKey(Product)
    sku = models.CharField(max_length=55, blank=True, null=True)
    barcode = models.CharField(max_length=55, blank=True, null=True)
    selling_price = models.DecimalField(max_length=15, decimal_places=2)

如何创建组合产品模型? 组合产品模型内部可以包含不同的创建产品(及其数量)。价格由用户指定。以下是我所拥有的,但我不知道如何处理。

class CombinedProduct(models.Model):
    product = models.ForeignKey(Product)
    item = models.ForeignKey(StandardProduct)
    quantity = models.DecimalField(max_length=15, decimal_places=2)

【问题讨论】:

    标签: django postgresql django-models


    【解决方案1】:

    如何创建 CombinedProduct 模型?结合的 产品模型里面可以有不同的创建产品(用他们的 数量)。价格由用户指定。下面是我所拥有的, 但我不知道如何处理。

    您可能想要的是带有through 选项的ManyToManyField

    这只是一个非常粗略的草图 - 我不了解您的 Product/StandardProduct 和关系。

    class CombinedProduct(models.Model):
        products = models.ManyToManyField(
            Product,
            through='Combination'
        )
    
    
    class Combination(models.Model):
        product = models.ForeignKey(Product)
        combined_product = models.ForeignKey(CombinedProduct)
        price = models.DecimalField(max_length=15, decimal_places=2)
        quantity = models.DecimalField(max_length=15, decimal_places=2)    
    

    【讨论】:

    • StandardProduct 模型有一个产品的变体,例如:颜色(类型)、红色(名称)、500(销售价格)等等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多