can't access to members of ManyToManyField

Question:

I want to get the products which are connect to my form like:[‘shirt’ , ‘hat’]
but this gives me the bound method Manager insted of Queryset.

models.py:

from django.db import models
from eshop_product.models import Product

class Form(models.Model):
    user_name = models.CharField(max_length=600, null=True, blank=True)
    first_name = models.CharField(max_length=600, null=True, blank=True)
    products = models.ManyToManyField(Product, blank=True)

views.py:

def checkOut(request):
    user = request.user
    products = Form.objects.filter(user_name=user.username)[0].products.all
    for product in products:
        print(product.title)

This is the returned error:

'ManyRelatedManager' object is not iterable
Asked By: Alireza

||

Answers:

You are doing it in view, not in template, so try doing with all() method rather than only all, so try this view:

views.py

def checkOut(request):
    user = request.user
    products = Form.objects.filter(user_name=user.username)[0].products.all()
    for product in products:
        print(product.title)

Note: Form is an in-built class in forms library, so change its name from Form to something like UserForm or anything suits to you.

Answered By: Sunderam Dubey