Home > Software design >  TypeError at /productdetails/20 'Products' object is not iterable
TypeError at /productdetails/20 'Products' object is not iterable

Time:07-18

I am doing CRUD using serializers and I am trying to make a page which will display the details of the clothes that I have clicked on. The below error is coming enter image description here

below is the productdetails function

def productdetails(request,id):
    prod = Products.objects.get(id=id)
    product = POLLSerializer(prod,many=True)
    return render(request,'polls/productdetails.html',{'data':product.data})

model

class Products(models.Model):
    categories = models.ForeignKey(Categories,on_delete=models.CASCADE)
    sub_categories = models.ForeignKey(SUBCategories,on_delete=models.CASCADE)
    color = models.ForeignKey(Colors,on_delete=models.CASCADE)
    size = models.ForeignKey(Size,on_delete=models.CASCADE)
    image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
    title = models.CharField(max_length=70)
    price = models.CharField(max_length=10)
    sku_number = models.CharField(max_length=10)
    product_details = models.CharField(max_length=1000)
    quantity = models.IntegerField(default=0)
    isactive = models.BooleanField(default=True)

below is productdetails.html ,since I need details of only 1 product,there is no need for loops hence I didnt add for loop

<table> 
  <tbody>
      <tr>
        <td>{{data.id}}</td>
        <td>{{data.title}}</td>
        <td>{{data.price}}</td>
        <td>{{data.sku_number}}</td>
        <td>{{data.product_details}}</td>
        <td>{{data.size}}</td>
        <td>{{data.quantity}}</td>
        <td>{{data.image}}</td>
       </tr>
      </tbody>
</table>

help is greatly appreciated,thanks!

CodePudding user response:

You are using many=True for a single object as @enes said. The argument many is used when you want to serialize a queryset. In this case you only want to serialize one object. Change to this:

product = POLLSerializer(prod)
  • Related