Advertisement
horozov86

example

Dec 6th, 2023
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. class Category(models.Model):
  2.     name = models.CharField(max_length=100)
  3.    
  4.    
  5. class Product(models.Model):
  6.     name = models.CharField(max_length=100)
  7.     description = models.TextField(null=True, blank=True)
  8.     price = models.DecimalField(max_digits=10, decimal_places=2)
  9.     category = models.ForeignKey("Category", on_delete=models.CASCADE) # ONE TO MANY ВРЪЗКА С Category
  10.     is_available = models.BooleanField(default=True)
  11.    
  12.     def __str__(self):
  13.  
  14.         return f"{self.category.name}: {self.name}"
  15.        
  16. class Customer(models.Model):
  17.     username = models.CharField(max_length=50, unique=True)
  18.  
  19.    
  20. class Order(models.Model):
  21.     customer = models.ForeignKey("Customer", on_delete=models.CASCADE) # ONE TO MANY ВРЪЗАКА С Customer
  22.     products = models.ManyToManyField("Product", through="OrderProduct") # MANY TO MANY ВРЪЗКА С Product
  23.  
  24.    
  25. class OrderProduct(models.Model):
  26.     order = models.ForeignKey("Order", on_delete=models.CASCADE) # ONE TO MANY ВРЪЗКА С Order
  27.     product = models.ForeignKey("Product", on_delete=models.CASCADE) # ONE TO MANY ВРЪЗКА С Product
  28.     quantity = models.PositiveIntegerField()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement