gpt4 book ai didi

python - “元组”对象在更新图像对象时没有属性 '_committed' 错误?

转载 作者:行者123 更新时间:2023-12-03 14:21:40 25 4
gpt4 key购买 nike

在这里,我试图更新特定产品的每个产品图像。但它不能正常工作。这里只有第一个对象的图像正在更新。
有一个模板,我们可以在其中一次更新产品和产品图片。 ProductImage有一个 ManyToOneProduct 的关系模型因此在模板中可以有单个产品的多个图像对象。
更新产品模型工作正常,但在更新 ProductImage 对象时它不起作用。
除了压缩,还有其他方法可以一次更新多个图像对象吗?
编辑:如果我解压缩图像列表,则更新无法正常工作。例如,如果我更改一个对象的图像,则另一个对象的图像也会更改。
但是 当我更改所有图像对象图像时,更新工作正常。当我只更改某些对象时,它无法正常工作。
当我压缩然后图像列表时,这是错误回溯。

Traceback (most recent call last):
File "venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\venv\lib\site-packages\django\views\generic\base.py", line 73, in view
return self.dispatch(request, *args, **kwargs)
File "\venv\lib\site-packages\django\views\generic\base.py", line 101, in dispatch
return handler(request, *args, **kwargs)
File "dashboard\product\views\views.py", line 293, in post
p.save()
File "venv\lib\site-packages\django\db\models\base.py", line 751, in save
force_update=force_update, update_fields=update_fields)
File "venv\lib\site-packages\django\db\models\base.py", line 789, in save_base
force_update, using, update_fields,
File "venv\lib\site-packages\django\db\models\base.py", line 867, in _save_table
for f in non_pks]
File "lib\site-packages\django\db\models\base.py", line 867, in <listcomp>
for f in non_pks]
File "\venv\lib\site-packages\django\db\models\fields\files.py", line 303, in pre_save
if file and not file._committed:
AttributeError: 'tuple' object has no attribute '_committed'
楷模
class ProductImage(models.Model):
image = models.ImageField(upload_to='imgs',blank=True, null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
模板
 <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">
意见
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)

最佳答案

一、改input name能够识别哪个ProductImage已更新。

<!-- <td><input type="file" name="image"></td> -->
<td><input type="file" name="image-{{image.pk}}"></td>
接下来,迭代 input_namerequest.FILES并获得 ProductImage PK。
然后,查找 ProductImage p ,更新 image场和 save该模型。
def post(self, request, *args, **kwargs):
product = Product.objects.get(pk=kwargs['pk'])
product_form = ProductForm(request.POST, instance=product)
if product_form.is_valid():
product_form.save()

# Updating product images
if request.FILES:
p_images = ProductImage.objects.filter(product=product).order_by('pk')
p_images_lookup = {p_image.pk: p_image for p_image in p_images}
for input_name in request.FILES:
p = p_images_lookup[int(input_name[len('image-'):])]
p.image = request.FILES[input_name]
p.save()

关于python - “元组”对象在更新图像对象时没有属性 '_committed' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64331384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com