You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.0 KiB
34 lines
1.0 KiB
1 year ago
|
from django.db import models
|
||
|
|
||
|
|
||
|
class User(models.Model):
|
||
|
username = models.CharField(max_length=255)
|
||
|
password = models.CharField(max_length=255)
|
||
|
balance = models.IntegerField()
|
||
|
is_vip = models.BooleanField()
|
||
|
|
||
|
|
||
|
class Product(models.Model):
|
||
|
name = models.CharField(max_length=255)
|
||
|
price = models.DecimalField(max_digits=8, decimal_places=2)
|
||
|
stock = models.IntegerField()
|
||
|
|
||
|
|
||
|
class Order(models.Model):
|
||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||
|
is_paid = models.BooleanField()
|
||
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||
|
products = models.ManyToManyField(Product, through='OrderItem')
|
||
|
|
||
|
@property
|
||
|
def total_price(self):
|
||
|
order_items = self.orderitem_set.all()
|
||
|
total = sum(item.product.price * item.quantity for item in order_items)
|
||
|
return total
|
||
|
|
||
|
|
||
|
class OrderItem(models.Model):
|
||
|
order = models.ForeignKey(Order, on_delete=models.CASCADE)
|
||
|
product = models.ForeignKey(Product, on_delete=models.CASCADE)
|
||
|
quantity = models.PositiveIntegerField()
|