当我想导入CSV文件格式的变体时,它不能接受JSON格式的变体。
@api_view(["POST"])
@permission_classes((permissions.IsAuthenticated,))
def CSVBulkUpdate(request, model, pk=None):
user = User.objects.get(id=request.user.id)
mapped_obj_to_import = []
data = request.data["data"]
mapped_obj = request.data["mapped_obj"]
print(data)
for row in data:
row_obj = {}
if row.get("id") or row.get("name"):
for key, item in mapped_obj.items():
if item and row.get(key) != "":
row_obj[item] = covertObjects(item, row.get(key))
mapped_obj_to_import.append(row_obj)
try:
if model == "product":
csvBulkUpdate(user.id, mapped_obj_to_import)
else:
csvVariantBulkUpdate(user.id, mapped_obj_to_import)
except Exception as e:
print("Error: ", str(e))
return Response({"data": str(e)}, status=400)
return Response({"data": "success"}, status=200)
csvVariantBulkUpdate函数代码:
def csvVariantBulkUpdate(user, dataToUpdate):
serializer_class = serializers.VariantImportUpdateSerializer
model = models.Variant
serializer_unapproved_class = serializers.UnapprovedVariantImportSerializer
module = "variant"
view = "Variant"
list_data = dataToUpdate
user = User.objects.get(id=user)
print(user)
variants = []
relational_fields = ["size", "color"]
results = {"success": {}, "failure": {}}
for i, item in enumerate(list_data):
results["failure"][item["id"]] = {}
try:
record = model.objects.get(id=item["id"])
item["product"] = record.product.id
if user.is_admin:
print("variant update admin view triggered")
update_size_in_approved = False
update_color_in_approved = False
if "size" in item:
error, data = sizesValidate(item["size"])
if error:
results["failure"][item["id"]]["size"] = data
item["size"] = data
update_size_in_approved = True
if "color" in item:
error, data = colorValidate(item["color"])
if error:
results["failure"][item["id"]]["color"] = data
item["color"] = data
update_color_in_approved = True
if len(results["failure"][item["id"]]) == 0:
serializer = serializer_class(record, data=item)
if serializer.is_valid():
for (key, value) in item.items():
if key not in ["id", "product"]:
if (
key in relational_fields
and update_size_in_approved
and update_color_in_approved
):
if update_size_in_approved:
value = get_size_instance(value)
elif update_color_in_approved:
value = get_color_instance(value)
data = {
"field": key,
"variant": record,
"approved": True,
key: value,
}
else:
data = {
"field": key,
"variant": record,
"approved": True,
key: value,
}
try:
models.ApprovedVariant.objects.get(
variant_id=item.get("id"),
field=key,
)
models.ApprovedVariant.objects.filter(
variant_id=item.get("id"),
field=key,
).update(**data)
except models.ApprovedVariant.DoesNotExist:
models.ApprovedVariant.objects.create(**data)
serializer.save()
# Activity logging
content = "%s updated a %s with ID %s" % (
user.name,
module,
item["id"],
)
create_activity(user, "Update", module, record.id, content)
variants.append(record.id)
results["success"][
item["id"]
] = "Variant has been updated successfully."
else:
print("errors:: ", serializer.errors)
results["failure"][item["id"]] = serializer.errors
else:
item[module] = record.id
product = serializers.VariantSerializer(record)
data = product.data
data["size"] = record.size
data["color"] = record.color
data[module] = record
if "hidden" in data:
del data["hidden"]
if "id" in data:
del data["id"]
update_size_in_approved = False
update_color_in_approved = False
if "size" in item:
error, data = sizesValidate(item["size"])
if error:
results["failure"][item["id"]]["size"] = data
item["size"] = data
update_size_in_approved = True
if "color" in item:
error, data = colorValidate(item["color"])
if error:
results["failure"][item["id"]]["color"] = data
item["color"] = data
update_color_in_approved = True
if len(results["failure"][item["id"]]) == 0:
data_unapproved = models.UnapprovedVariant.objects.create(**data)
serializer = serializer_unapproved_class(data_unapproved, data=item)
if serializer.is_valid():
serializer.validated_data["unapproved"] = True
for (key, value) in item.items():
if (
key in relational_fields
and update_size_in_approved
and update_color_in_approved
):
if update_size_in_approved:
value = get_size_instance(value)
elif update_color_in_approved:
value = get_color_instance(value)
data3 = {
"field": key,
"variant": record,
"approved": True,
key: value,
}
else:
data3 = {
"field": key,
"variant": record,
"approved": True,
key: value,
}
try:
models.ApprovedVariant.objects.get(
variant_id=item.get("id"),
field=key,
)
models.ApprovedVariant.objects.filter(
variant_id=item.get("id"),
field=key,
).update(**data)
except models.ApprovedVariant.DoesNotExist:
models.ApprovedVariant.objects.create(**data3)
result = serializer.save().id
message = (
"%s updated a %s with ID %s \n approve to show in the list."
% (user.name, module, record.id)
)
admins = User.objects.filter(is_admin=True)
for admin in admins:
notification = Notifications(
sent_by=user,
user=admin,
message=message,
content_id=record.id,
module=view,
action="Update",
unapproved_id=result,
)
notification.save()
# Activity logging
content = "%s updated a %s with ID %s" % (
user.name,
module,
record.id,
)
create_activity(user, "Update", module, record.id, content)
variants.append(record.id)
results["success"][
item["id"]
] = "Variant update request has been added successfully for approval."
else:
results["failure"][item["id"]] = serializer.errors
except Exception as e:
print("An error occured: ", str(e))
results["failure"][item["id"]] = str(e)
has_error = 0
if len(results["failure"]):
results["import_success"] = False
has_error = 1
# save update progress
models.UpdateProgress.objects.create(
user=user,
productsCount=len(variants),
response=results,
has_error=has_error,
is_read=False,
)
import_variant_response(user.id, results, "Update")
return results
我应用了yaml,但没有工作。我也使用force_text到一个对象,但没有解决我的问题。当导入文件“Bad Request:/API/v1/variant/bulk/import_update/”和“类型为”proxy“的对象无法进行JSON序列化”,此错误显示在终端中。
1条答案
按热度按时间omqzjyyz1#
错误是由函数中的以下行引起的:
request.data 未采用预期格式,或者未包含预期数据。能否更新有关要发送到此终结点的request.data对象的格式的问题?