开发者

django manytomany relation : 'tuple' object has no attribute 'all'

开发者 https://www.devze.com 2023-03-24 02:45 出处:网络
I\'v got problem with \'self\' manytomany relation in Django: My models : c开发者_Go百科lass EcomProduct(models.Model):

I'v got problem with 'self' manytomany relation in Django:

My models :

c开发者_Go百科lass 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'

Now in views.py I need to get all products related to the particular product:

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     

And I receive message:

Exception Value: 'tuple' object has no attribute 'all'

I have no idea how to fix it. Question is how to get all related products to the particular product ? Thanks for help in advance.


You have a comma after the declaration of programs in your EcomProduct model definition. That turns it into a tuple, which you certainly don't want.

0

精彩评论

暂无评论...
验证码 换一张
取 消