first commit
This commit is contained in:
33
cart/models.py
Normal file
33
cart/models.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from product.models import Product
|
||||
|
||||
class Cart(models.Model):
|
||||
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='cart')
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user.email} - Cart"
|
||||
|
||||
def get_total_price(self):
|
||||
return sum(item.total_price for item in self.items.all())
|
||||
|
||||
class CartItem(models.Model):
|
||||
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name='items')
|
||||
product = models.ForeignKey(Product, on_delete=models.CASCADE)
|
||||
quantity = models.PositiveIntegerField(default=1)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('cart', 'product')
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.cart.user.email} - {self.product.title}"
|
||||
|
||||
@property
|
||||
def price(self):
|
||||
return self.product.price
|
||||
|
||||
@property
|
||||
def total_price(self):
|
||||
return self.price * self.quantity
|
||||
Reference in New Issue
Block a user