我在 Django 中遇到了“自我”多对多关系的问题:
我的模型:
class EcomProduct(models.Model):
products = models.ManyToManyField('self', through='EcomProductToProduct', symmetrical=False),
class EcomProductToProduct(models.Model):
from_ecom_product = models.ForeignKey(EcomProduct,related_name='from_ecomproduct')
to_ecom_product = models.ForeignKey(EcomProduct,related_name='to_ecomproduct')
class Meta:
db_table = u'ecom_product_to_product'
现在在 views.py 中,我需要获取与特定产品相关的所有产品:
def show_product(request, ecomproduct_slug, template_name="catalog/product.html"):
p = get_object_or_404(EcomProduct, slug=ecomproduct_slug)
related_products_to_product = p.products.all() #it doesn't work
我收到消息:
Exception Value: 'tuple' object has no attribute 'all'
我不知道如何解决它。问题是如何获得特定产品的所有相关产品?提前感谢您的帮助。