T
T
Tinord2018-06-21 16:47:43
Django
Tinord, 2018-06-21 16:47:43

How to implement price, type and product models?

Hello.
Such a question: there is a model of products, types, type attributes.
It is necessary to implement the price model in such a way that the types are common for all products, and the price must refer to both the product and each attribute of the type.
I will give an example: product - ice cream, type - cone, attributes - small, medium, large. The price accordingly varies from attribute and product.
I look towards contenttype

class ProductAttribute(models.Model):
    name = models.CharField(max_length=128, verbose_name="Название атрибута")

    class Meta:
        verbose_name = 'Атрибут'
        verbose_name_plural = 'Атрибуты'


class ProductType(models.Model):
    name_type = models.CharField(max_length=128, verbose_name="Название типа продукта")
    product_attribute = models.ManyToManyField(ProductAttribute)

    class Meta:
        verbose_name = 'Тип'
        verbose_name_plural = 'Типы'

    def __str__(self):
        return self.name_type

class Product(models.Model):
    category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name="Категория")
    type = models.ForeignKey(ProductType, on_delete=models.PROTECT, verbose_name="Тип")
    tags = models.ManyToManyField(Tags, )
    name = models.CharField(max_length=200, db_index=True, verbose_name="Название")
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d/', blank=True, verbose_name="Изображение товара")
    description = models.TextField(blank=True, verbose_name="Описание")
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['name']
        verbose_name = 'Продукт'
        verbose_name_plural = 'Продукты'
        index_together = [
            ['id', 'slug']
        ]

    def __str__(self):
        return self.name

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question