Advertisement
Guest User

models.py

a guest
Apr 5th, 2021
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. from django.db import models
  2. choices_cat = [('other', 'Other'), ('play', 'Play'), ('electronic', 'Electronic'), ('house', 'House')]
  3.  
  4. # Create your models here.
  5.  
  6. class Product(models.Model):
  7. name = models.CharField(max_length=100, null=False, blank=False)
  8. description = models.TextField(max_length=2000, null=True, blank=True)
  9. category = models.CharField(max_length=100, null=False, blank=False, default='other', choices=choices_cat)
  10. the_remainder = models.IntegerField(null=False, blank=False, default=0)
  11. price = models.DecimalField(max_digits=10, decimal_places=2, null=False, blank=False)
  12.  
  13. class Meta:
  14. db_table = 'products'
  15. verbose_name = 'Продукт'
  16. verbose_name_plural = 'Продукты'
  17.  
  18. def __str__(self):
  19. return f'{self.id}. {self.name}: {self.category}'
  20.  
  21.  
  22. class Basket(models.Model):
  23. product = models.ForeignKey('webapp.Product', on_delete=models.CASCADE, related_name='product', verbose_name='Продукт')
  24. qty = models.IntegerField(null=False, blank=False, default=1)
  25.  
  26. class Meta:
  27. db_table = 'basket'
  28. verbose_name = 'Корзина'
  29. verbose_name_plural = 'Корзины'
  30.  
  31. def __str__(self):
  32. return f'{self.id}. {self.product}: {self.qty}'
  33.  
  34. class OrderProduct(models.Model):
  35. product = models.ForeignKey('webapp.Product', on_delete=models.CASCADE, related_name='product_o', verbose_name='Продукт')
  36. order = models.ForeignKey('webapp.Order', on_delete=models.CASCADE, related_name='order_o', verbose_name='Заказ')
  37. qty = models.IntegerField(null=False, blank=False)
  38.  
  39. class Order(models.Model):
  40. products = models.ManyToManyField('webapp.Product', related_name='products', through='webapp.OrderProduct', through_fields=('product', 'order'))
  41. name = models.CharField(max_length=200, null=False, blank=False)
  42. phone = models.CharField(max_length=300, null=False, blank=False)
  43. the_address = models.CharField(max_length=300, null=False, blank=False)
  44. created_at = models.DateTimeField(auto_now_add=True)
  45.  
  46. class Meta:
  47. db_table = 'order'
  48. verbose_name = 'Заказ'
  49. verbose_name_plural = 'Заказы'
  50.  
  51. def __str__(self):
  52. return f'{self.id}. {self.products}: {self.name} - {self.phone}'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement