Advertisement
horozov86

Advanced Django Model Techniques 1-3

Nov 23rd, 2023 (edited)
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. # 1. Customer
  2.  
  3. def validator(value):
  4.     for char in value:
  5.         if not char.isalpha() or not char.isspace():
  6.             raise ValidationError("Name can only contain letters and spaces")
  7.            
  8. def phone_validator(value):
  9.     if not re.match(r'^\+359\d{9}$', value):
  10.         raise ValidationError("Phone number must start with a '+359' followed by 9 digits")
  11.    
  12.  
  13. class Customer(models.Model):
  14.     name = models.CharField(max_length=100, validators=[validator])
  15.     age = models.PositiveIntegerField(validators=[MinValueValidator(18), message="Age must be greater than 18"])
  16.     email = models.EmailFiled(error_message = {'invalid': "Enter a valid email address"})
  17.     phone_number = models.CharField(max_length=13, validatiors=[phone_validator])
  18.     website_url = models.URLField(error_message = {'invalid': "Enter a valid URL"})
  19.    
  20. # 2.    Media
  21.  
  22.  
  23. class BaseMedia(models.Model):
  24.     title = models.CharField(max_length=100)
  25.     description = models.TextField()
  26.     genre = models.CharField(max_length=50)
  27.     created_at = models.DateTimeField(auto_now_add=True)
  28.    
  29.     class Meta:
  30.         abstract= True
  31.         ordering = ["-created_at", "title"]
  32.        
  33.        
  34. class Book(BaseMedia):
  35.     author = models.CharField(max_length=100, validators=[MinLengthValidator(5, message="Author must be at least 5 characters long")])
  36.     isbn = models.CharField(max_length=20, unique=True, validators=[MinLengthValidator(6, message="ISBN must be at least 6 characters long")])
  37.    
  38.     class Meta(BaseMedia.Meta):
  39.         verbose_name = "Model Book"
  40.         verbose_name_plural = "Models of type - Book"
  41.    
  42. class Movie(BaseMedia):
  43.     director = models.CharField(max_length=100, validators=[MinLengthValidator(8, message="Director must be at least 8 characters long")])
  44.    
  45.     class Meta(BaseMedia.Meta):
  46.         verbose_name = "Model Movie"
  47.         verbose_name_plural = "Models of type - Movie"
  48.    
  49.    
  50. class Music(BaseMedia):
  51.     artist = models.CharField(max_length=100, validators=[MinLengthValidator(9, message="Artist must be at least 9 characters long")])
  52.    
  53.     class Meta(BaseMedia.Meta):
  54.         verbose_name = "Model Music"
  55.         verbose_name_plural = "Models of type - Music "
  56.        
  57.        
  58. # 3.    Digital Products
  59.  
  60. class Product(models.Model):
  61.     name = models.CharField(max_length=100)
  62.     price = models.DecimaField(max_digits=10, decimal_places=2)
  63.    
  64.     def calculate_tax(self):
  65.         return self.price * Decimal(0.08)
  66.         # да проверя без Decimal дали минава
  67.        
  68.     def calculate_shipping_cost(weight: Decimal):
  69.         return weight * Decimal(2.00)
  70.        
  71.     def format_product_name(self):
  72.         return f"Product: {self.name}"
  73.        
  74.    
  75. # когато моделът Child споделя същата таблица като Parent class-a предоставя допълнителна функционалност, то той е class Meta: е proxy
  76.  
  77. class DiscountedProduct(Product):
  78.     class Meta:
  79.         proxy = True
  80.        
  81.     def calculate_price_without_discount(self):
  82.         return self.price  * Decimal(1.20)
  83.        
  84.     def calculate_tax(self):
  85.         return self.price * Decimal(0.05)
  86.         # да проверя без Decimal дали минава
  87.        
  88.     def calculate_shipping_cost(weight: Decimal):
  89.         return weight * Decimal(1.50)
  90.        
  91.     def format_product_name(self):
  92.         return f"Discounted Product: {self.name}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement