django_magazine/cart/models.py

28 lines
855 B
Python
Raw Permalink Normal View History

2023-01-31 11:20:57 +00:00
from django.db import models
from shop.models import Product
from shop.models import Variation
from accounts.models import Account
class Cart(models.Model):
cart_id = models.CharField(max_length=250, blank=True)
create = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.cart_id
class CartItem(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE, null=True )
product = models.ForeignKey(Product, on_delete=models.CASCADE)
variation = models.ManyToManyField(Variation, blank=True)
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)
quantity = models.IntegerField()
is_active = models.BooleanField(default=True)
def sub_total(self):
return self.product.price * self.quantity
def __unicode__(self):
return self.product