Untitled
unknown
plain_text
a year ago
1.4 kB
8
Indexable
from django.core.validators import MinValueValidator, MinLengthValidator
from django.db import models
from products.models import Product
from suppliers.models import Supplier
# Create your models here.
class Price(models.Model):
PRICE_DIGITS = 10
PRICE_DECIMAL_DIGITS = 2
MAX_LENGTH_NAME = 15
MIN_LENGTH_NAME = 3
Categories = {
"video_card": 1.05,
"hdd": 1.07,
'processors': 1.04
}
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product', null=True)
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, related_name='supplier', null=True)
name = models.CharField(
max_length=MAX_LENGTH_NAME,
validators=[
MinLengthValidator(MIN_LENGTH_NAME, message="Name must be between 3 and 15 characters"),
],
blank=False,
null=False
)
category = models.CharField(max_length=20, validators=[MinLengthValidator(3)])
price = models.DecimalField(
max_digits=PRICE_DIGITS,
decimal_places=PRICE_DECIMAL_DIGITS,
)
priority = models.IntegerField(validators=[MinValueValidator(1)])
def save(self, *args, **kwargs):
if self.category in self.Categories.keys():
self.price = self.price * self.Categories[self.category]
super().save(*args, **kwargs)Editor is loading...
Leave a Comment