Как сохранить значение в структуре, как в C в Django Model - PullRequest
0 голосов
/ 14 января 2019
***class Product(models.Model):
    product_id = models.IntegerField(primary_key=True)
    name = models.TextField()
    length = models.FloatField()
    width = models.FloatField()
    depth = models.FloatField()
    color_text = models.TextField()
    color_rgb = models.ForeignKey(ColorRGB, on_delete=models.SET_NULL, null=True)
    color_cmyk = models.ForeignKey(ColorCMYK, on_delete=models.SET_NULL, null=True)
    treatment_text = models.TextField()
    treatment_number = models.FloatField()
    transparency_text = models.TextField()
    transparency_number = models.FloatField()
    total_carat_weight = models.FloatField()
    country_of_origin = models.TextField()
    hardness_text = models.TextField()
    hardness_number = models.FloatField()
    pieces_text = models.TextField(
    pieces_number = models.IntegerField()
    price_pkr = models.FloatField()***

см. Поля color_rgb и color_cmyk. Один имеет 3 целочисленных значения для хранения, а другой имеет 4 целочисленных значения для хранения.

Я пытаюсь определить это как ниже. Вопрос в том, что это правильный путь или есть лучший способ сделать это?


class ColorRGB(models.Model):
    R = models.IntegerField()
    G = models.IntegerField()
    B = models.IntegerField()

1 Ответ

0 голосов
/ 17 января 2019

Я предлагаю сделать это по-другому - модель ColorRGB ссылается на Product с внешним ключом:

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()

Затем вы можете обращаться к цветам таким же образом, например, Product.color_rgb и т. Д. Вы также можете определить подмодели подмоделей, если хотите.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...