from django.db import models from api.models import Product from django.contrib.auth.models import User class Cart(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, related_name='cart', null=True, blank=True ) session_key = models.CharField(max_length=40, null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): if self.user: return f"Cart for {self.user.username}" return f"Anonymous Cart ({self.session_key})" def get_total(self): return sum(item.get_subtotal() 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) size = models.CharField(max_length=20, blank=True) color = models.CharField(max_length=20, blank=True) added_at = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ['cart', 'product', 'size', 'color'] def __str__(self): return f"{self.product.name} x {self.quantity}" def get_subtotal(self): return self.product.price * self.quantity