from django.contrib.auth.models import User from django.db import models from django.conf import settings class ColorChoices(models.TextChoices): RED = "Red", "Red" BLUE = "Blue", "Blue" GREEN = "Green", "Green" BLACK = "Black", "Black" WHITE = "White", "White" class SizeChoices(models.TextChoices): SMALL = "S", "Small" MEDIUM = "M", "Medium" LARGE = "L", "Large" XLARGE = "XL", "X-Large" class Color(models.Model): name = models.CharField(max_length=10, choices=ColorChoices.choices, unique=True) def __str__(self): return self.name class Size(models.Model): name = models.CharField(max_length=5, choices=SizeChoices.choices, unique=True) def __str__(self): return self.name class Product(models.Model): title = models.CharField(max_length=200) image = models.ImageField(upload_to="products/", null=True, blank=True) price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00) # Use string references to prevent circular imports colors = models.ManyToManyField("shop.Color", blank=True, related_name="products") sizes = models.ManyToManyField("shop.Size", blank=True, related_name="products") def __str__(self): return self.title class CartItem(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True) product = models.ForeignKey(Product, on_delete=models.CASCADE) quantity = models.PositiveIntegerField(default=1) session_id = models.CharField(max_length=255, null=True, blank=True) date_added = models.DateTimeField(auto_now_add=True) def __str__(self): return f"{self.quantity} x {self.product.title}" def get_total_price(self): return self.product.price * self.quantity