python – How to update multiple images at a time?


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.

models

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

template

{% for image in p_images %}
<tr><td>{{image.pk}}</td>
<td><img src="https://stackoverflow.com/{{image.image.url}}" width="50" height="50"></td>
<td>&nbsp;&nbsp;<input type="file" name="image"></td>
</tr>
{% endfor %}

views

images = request.FILES.getlist('image')
p_images = ProductImage.objects.filter(product=product).order_by('pk')
for p, img in zip(p_images, images):  
  p.image = img
  p.save()
  # Tried this way too:

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