models.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # models.py
  2. from django.db import models
  3. from django.contrib.postgres.fields import JSONField
  4. class Product(models.Model):
  5. title = models.CharField(max_length=500)
  6. description = models.TextField()
  7. short_description = models.TextField(blank=True)
  8. attributes_extracted = models.BooleanField(default=False)
  9. created_at = models.DateTimeField(auto_now_add=True)
  10. updated_at = models.DateTimeField(auto_now=True)
  11. class Meta:
  12. db_table = 'products'
  13. indexes = [
  14. models.Index(fields=['attributes_extracted', 'created_at']),
  15. ]
  16. class ProductImage(models.Model):
  17. product = models.ForeignKey(Product, related_name='images', on_delete=models.CASCADE)
  18. image = models.ImageField(upload_to='products/')
  19. order = models.PositiveIntegerField(default=0)
  20. class Meta:
  21. db_table = 'product_images'
  22. ordering = ['order']
  23. class ProductAttribute(models.Model):
  24. product = models.ForeignKey(Product, related_name='attributes', on_delete=models.CASCADE)
  25. attribute_name = models.CharField(max_length=100, db_index=True)
  26. attribute_value = models.TextField()
  27. confidence_score = models.FloatField(default=0.0)
  28. extraction_method = models.CharField(
  29. max_length=20,
  30. choices=[('nlp', 'NLP'), ('llm', 'LLM'), ('hybrid', 'Hybrid')],
  31. default='hybrid'
  32. )
  33. needs_review = models.BooleanField(default=False)
  34. reviewed = models.BooleanField(default=False)
  35. created_at = models.DateTimeField(auto_now_add=True)
  36. class Meta:
  37. db_table = 'product_attributes'
  38. unique_together = ['product', 'attribute_name']
  39. indexes = [
  40. models.Index(fields=['attribute_name', 'confidence_score']),
  41. models.Index(fields=['needs_review', 'reviewed']),
  42. ]
  43. def save(self, *args, **kwargs):
  44. # Auto-flag low confidence for review
  45. if self.confidence_score < 0.7:
  46. self.needs_review = True
  47. super().save(*args, **kwargs)