Python 3.x 在django模型中如何像C一样在结构中存储值

Python 3.x 在django模型中如何像C一样在结构中存储值,python-3.x,django-models,Python 3.x,Django Models,请参见颜色rgb字段和颜色cmyk字段。一个有3个要存储的整数值,另一个有4个要存储的整数值 我试图定义如下。问题是这是正确的方法还是有更好的方法 我建议以另一种方式进行操作-使用外键将ColorRGB型号参考产品: class ColorRGB(models.Model): R = models.IntegerField() G = models.IntegerField() B = models.IntegerField() 然后,您可以使用相同的方式参考颜色,例

请参见颜色rgb字段和颜色cmyk字段。一个有3个要存储的整数值,另一个有4个要存储的整数值

我试图定义如下。问题是这是正确的方法还是有更好的方法




我建议以另一种方式进行操作-使用外键将
ColorRGB
型号参考
产品

class ColorRGB(models.Model):
    R = models.IntegerField()
    G = models.IntegerField()
    B = models.IntegerField()
然后,您可以使用相同的方式参考颜色,例如使用
Product.color\u rgb
等。如果愿意,您还可以定义子模型的子模型

class ColorRGB(models.Model):
    R = models.IntegerField()
    G = models.IntegerField()
    B = models.IntegerField()
class Product(models.Model):
    product_id = models.IntegerField(primary_key=True)
    # All the other product fields as normal, except the color ones


class ColorRGB(models.Model):
    product = models.ForeignKey(Product, related_name='color_rgb',
                                on_delete=models.CASCADE)
    R = models.IntegerField()
    G = models.IntegerField()
    B = models.IntegerField()


class ColorCMYK(models.Model):
    product = models.ForeignKey(Product, related_name='color_cmyk',
                                on_delete=models.CASCADE)
    C = models.IntegerField()
    M = models.IntegerField()
    Y = models.IntegerField()
    K = models.IntegerField()