‘tuple’ object has no attribute ‘_committed’ error while updating image objects?

Here I am trying to update each product image of a particular product. But it is not working properly. Here only the image of first object is updating.

There is a template where we can update product and product images at once. ProductImage has a ManyToOne relation with Product Model so in the template there can be multiple images objects of a single product.

Updating product model works fine but while updating ProductImage Objects it is not working.

Instead of zipping is there any other way to update multiple image objects at once ?

models

class ProductImage(models.Model):
    image = models.ImageField(upload_to='imgs',blank=True, null=True)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)

template

 <form method="post" enctype="multipart/form-data">
       {% csrf_token %}
       {{product_form}}
    <th>Current Image</th>
    <th>Change</th>
    {% for image in p_images %}
    <tr><td>{{image.pk}}</td>
    <td><img src="{{image.image.url}}" width="50" height="50"></td>
    <td><input type="file" name="image"></td>
    </tr>
    {% endfor %}
   <input type="submit">

views

def post(self, request, *args, **kwargs):
    product = Product.objects.get(pk=kwargs['pk'])
    product_form = ProductForm(request.POST, instance=product)
    images = zip(request.FILES.getlist('image')) 
    p_images = ProductImage.objects.filter(product=product).order_by('pk')
    if product_form.is_valid():
        product_form.save()             
        
       # updating product images 
        for p, img in zip(p_images, images):  # error is here
           p.image = img
           p.save()

           # Tried this way too:

       for img in images:
           ProductImage.objects.filter(product=product).update(image=img)


Read more here: https://stackoverflow.com/questions/64331384/tuple-object-has-no-attribute-committed-error-while-updating-image-objects

Content Attribution

This content was originally published by D_P at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: