Need your help to fix my database relationship in Django model - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Need your help to fix my database relationship in Django model (/thread-11299.html) |
Need your help to fix my database relationship in Django model - PrateekG - Jul-02-2018 Hi All, I have created a simple database admin panel (Django2.0.6+Python3.6+Mysql5.7) where admin can operate create/edit/delete operations of database. I am facing following IntegrityError error when I try to Merge two products and click on Preview from my Django admin panel- I think issue is may be wrong relationship settings in my model file but couldn't able to find exact issue.Can anyone please help me to find the issue? My code is as below- class MerchantProduct(models.Model): merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True) merchant_product_id = models.CharField(max_length=100) gtin = models.CharField(max_length=15, blank=True, null=True) merchant_group_id = models.CharField(max_length=100, blank=True, null=True) name = models.CharField(max_length=300) class Meta: unique_together = (('merchant', 'merchant_product_id'),) managed = False db_table = 'merchant_product' def __str__(self): return self.name class Product(models.Model): merchants = models.ManyToManyField(Merchant, through='MerchantProduct') brand = models.ForeignKey(Brand, related_name='brand', on_delete=models.SET_NULL, blank=True, null=True) category = models.ForeignKey(Category, related_name='category', on_delete=models.SET_NULL, blank=True, null=True) group = models.ForeignKey('self', related_name='ID', on_delete=models.SET_NULL, blank=True, null=True) gtin = models.CharField(max_length=15, blank=True, null=True) name = models.CharField(max_length=300) description = models.TextField() class Meta: managed = False db_table = 'product' def __str__(self): return self.name class Merchant(models.Model): DATA_QUALITY_CHOICES = ( ('approx', 'Approximate'), ('exact', 'Exact'), ) name = models.CharField(unique=True, max_length=45) display_name = models.CharField(unique=True, max_length=255) url_name = models.CharField(unique=True, max_length=255) class Meta: managed = False db_table = 'merchant' def __str__(self): return self.name class Feed(models.Model): merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE) merchant_store_id = models.CharField(max_length=100) merchant_product_id = models.CharField(max_length=100) class Meta: unique_together = (('merchant', 'merchant_store_id', 'merchant_product_id'),) managed = False db_table = 'feed' |