From b2e6a6cf1a80d65fc5999d14f2e0e382c9a1ebe1 Mon Sep 17 00:00:00 2001 From: Zixiao Wang Date: Mon, 26 May 2025 10:20:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=A4=E6=96=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/expertproducts/views.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/expertproducts/views.py b/apps/expertproducts/views.py index dc725b0..5e54efa 100644 --- a/apps/expertproducts/views.py +++ b/apps/expertproducts/views.py @@ -48,11 +48,26 @@ class ContentAnalysisAPI(APIView): ] 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( - {"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 + ) # 3. 解析 JSON 字段 try: