Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
1.7 kB
1
Indexable
Never
from django.db import models

from products.models import Category, Product
from suppliers.models import Supplier


# Create your models here.
class Prices(models.Model):
    Categories = {
        "Memory Flash cards": 1.07, #надценка 5%
        "Video cards": 1.04 #надценка 4%
    }

    ean = models.CharField(
        max_length=13,
        blank=True,
        null=True,
        unique=True
    )
    wic = models.CharField(
        max_length=255,
        unique=True
    )
    description = models.TextField()
    category = models.CharField(
        max_length=255
    )
    supplier_name = models.CharField(
        max_length=255
    )
    vpf_name = models.CharField(
        max_length=255,
        blank=True,
        null=True
    )
    currency_code = models.CharField(
        max_length=3
    )
    availability = models.CharField(
        max_length=255
    )
    retail_price = models.DecimalField(
        max_digits=10,
        decimal_places=2
    )
    my_price = models.DecimalField(
        max_digits=10,
        decimal_places=2
    )
    warranty_term = models.PositiveIntegerField()
    category_id = models.ForeignKey(
        Category, on_delete=models.CASCADE
    )
    supplier_id = models.ForeignKey(
        Supplier, on_delete=models.CASCADE
    )
    image = models.URLField()
    product = models.ForeignKey(
        Product, on_delete=models.CASCADE
    )

    def __str__(self):
        return self.description

    def save(self, *args, **kwargs):
        if self.category in self.Categories.keys():
            self.retail_price = self.my_price * self.Categories[self.category]
            super().save(*args, **kwargs)
Leave a Comment