我正在尝试使用Django Rest Framework更新图像字段。我覆盖了views.py中的更新方法。由于我正在进行批量更新,所以这就是为什么我使用了for循环并尝试更新每个字段的原因。问题是我不知道如何手动更新图像字段。因此,任何想法或建议都会有所帮助。
def update(self, request, *args, **kwargs):
instance = self.get_object()
response_data = { "kitchen_audit":[]}
if not isinstance(request.data, dict):
for kitchen_audit in self.request.data:
kitchen_audit_instance = KitchenAudit.objects.get(id = kitchen_audit["id"])
kitchen_audit_instance.marks_scored = kitchen_audit['marks_scored']
kitchen_audit_instance.comment = kitchen_audit['comment']
kitchen_audit_instance.save()
response_data["kitchen_audit"].append(
{'id': kitchen_audit['id'],
'marks_scored': kitchen_audit['marks_scored'],
'comment':kitchen_audit['comment']})
for kitchen_audit_image in kitchen_audit["images"]:
kitchen_audit_image_instance = KitchenAuditImage.objects.get (id=kitchen_audit_image["id"])
kitchen_audit_image_instance["image_url"] = kitchen_audit_image['image_url']
kitchen_audit_image_instance.save()