更新判断逻辑
This commit is contained in:
parent
bca9490fba
commit
b2e6a6cf1a
@ -48,9 +48,24 @@ class ContentAnalysisAPI(APIView):
|
|||||||
]
|
]
|
||||||
|
|
||||||
for field in required_fields:
|
for field in required_fields:
|
||||||
if field not in data:
|
value = data.get(field)
|
||||||
|
if value is None or (isinstance(value, str) and value.strip() == ""):
|
||||||
return Response(
|
return Response(
|
||||||
{"error": f"Field '{field}' is required."},
|
{"error": f"字段 '{field}' 不能为空"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
# 新增校验:price和stock必须为数字且>=0
|
||||||
|
if field in ["product_info.price", "product_info.stock"]:
|
||||||
|
try:
|
||||||
|
num_value = float(value)
|
||||||
|
if num_value < 0:
|
||||||
|
return Response(
|
||||||
|
{"error": f"'{field}' 不合法"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return Response(
|
||||||
|
{"error": f"'{field}' 不合法"},
|
||||||
status=status.HTTP_400_BAD_REQUEST
|
status=status.HTTP_400_BAD_REQUEST
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user