models.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from django.db import models
  2. class Product(models.Model):
  3. """
  4. Stores product details
  5. """
  6. item_id = models.CharField(max_length=100, unique=True)
  7. product_name = models.CharField(max_length=255)
  8. product_long_description = models.TextField(blank=True, null=True)
  9. product_short_description = models.TextField(blank=True, null=True)
  10. product_type = models.CharField(max_length=100, blank=True, null=True)
  11. image_path = models.CharField(max_length=500, blank=True, null=True)
  12. image = models.ImageField(upload_to='products/', blank=True, null=True)
  13. def __str__(self):
  14. return f"{self.product_name} ({self.item_id})"
  15. # models.py
  16. class ProductAttributeValue(models.Model):
  17. """
  18. Stores manually entered original attribute values for products
  19. """
  20. product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="attribute_values")
  21. attribute_name = models.CharField(max_length=100)
  22. original_value = models.TextField()
  23. created_at = models.DateTimeField(auto_now_add=True)
  24. updated_at = models.DateTimeField(auto_now=True)
  25. class Meta:
  26. unique_together = ['product', 'attribute_name']
  27. def __str__(self):
  28. return f"{self.product.item_id} - {self.attribute_name}: {self.original_value}"
  29. class ProductType(models.Model):
  30. name = models.CharField(max_length=100, unique=True)
  31. def __str__(self):
  32. return self.name
  33. class ProductAttribute(models.Model):
  34. product_type = models.ForeignKey(ProductType, on_delete=models.CASCADE, related_name="attributes")
  35. name = models.CharField(max_length=100)
  36. is_mandatory = models.BooleanField(default=False)
  37. def __str__(self):
  38. return f"{self.product_type.name} - {self.name} ({'Mandatory' if self.is_mandatory else 'Additional'})"
  39. class AttributePossibleValue(models.Model):
  40. attribute = models.ForeignKey(ProductAttribute, on_delete=models.CASCADE, related_name="possible_values")
  41. value = models.CharField(max_length=255)
  42. def __str__(self):
  43. return f"{self.attribute.name}: {self.value}"