My product model has two different relationship with category model:
Now the situation is when I tried to fetch using the second relationship, I am getting the result from my first relationship.
For example this is what I am trying to print:
Category.objects.get(slug="root").shadow_products.all()
but it converts to following sql :
print(Category.objects.get(slug="root").shadow_products.all().query)
SELECT `product_management_product`.`id`, `product_management_product`.`slug`, `product_management_product`.`category_id`, `product_management_product`.`brand_id` FROM `product_management_product` WHERE `product_management_product`.`category_id` = 720
My models looks like following:
class Category(SlugableModel):
#...
shadow_products = models.ManyToManyField("product_management.Product", through="product_management.ProductShadowCategory")
class Product(SlugableModel):
#...
category = models.ForeignKey(Category,on_delete=models.CASCADE, related_name="products", validators=[leaf_category])
class ProductShadowCategory(MyModel):
category = models.ForeignKey(Category,on_delete=models.CASCADE)
product = models.ForeignKey(Product,on_delete=models.CASCADE)
class Meta:
unique_together = ('category', 'product')
Also asked on stackoverflow: https://stackoverflow.com/questions/52417494/my-django-model-joining-wrong-fields-when-there-are-multiple-related-fields-with