如何从 python 序列化程序中引发的验证错误响应中删除 'b' 文字
How to remove 'b' literal from Raised validation error response in python serializer
当我在序列化程序中引发验证错误时,输出以 'b' 为前缀(我认为这是字节类型)并且我尝试删除它的所有方法都失败了。
我的序列化器class:
class AppInputTypeSerializer(serializers.Serializer):
#value = serializers.CharField(required=True, max_length = 100, min_length = 1)
def validate_value(self, data):
length = len(data['value'])
if length < 1 or length > 100:
message = "We are sorry, your reply should be between 1 and 100 characters. Please try again."
data['error'] = message
raise serializers.ValidationError(message)
return data
我的看法:
class PresentationLayerView(generics.GenericAPIView):
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
body = request.data
cardType = body['cardType']
if cardType == "TERMS_CONDITIONS":
serializer = AppTandCTypeSerializer(body)
elif cardType == "CHOICE":
serializer = AppChoiceTypeSerializer(body)
elif cardType == "TEXT" or cardType == "INPUT":
serializer = AppTextTypeSerializer(body)
serializer.validate_value(body)
#print(response.text)
return Response({}, status=status.HTTP_200_OK,)
Test_views:
class StartAssessmentViewTests(APITestCase):
# Return 如果用户为输入卡类型输入 > 100 个字符,则验证错误
def test_input_type(self):
#response = self.client.get(
# reverse("app-start-assessment")
#)
response = self.client.post(reverse("app-start-assessment"),
json.dumps({"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":4,
"value": "This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":8,
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT"}),
content_type='application/json')
self.assertEqual(
response.content,
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
}, response.json,
)
由于以下前缀,输出是断言失败:
b'{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}'
而不是这个:
{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}
请帮忙。谢谢
Output is an assert failure because of the prefix below.
没有,不是这个原因。原因是因为您正在将二进制字符串与字典进行比较。但即使它是一个简单的字符串,也会失败,因为字符串不是字典,即使它们看起来一样。
response.content
是二进制字符串,而不是字典,因为它最终会导致二进制流作为 HTTP 响应发送。
最好JSON对对象进行解码,然后检查是否与字典等价。您可以使用 response.json()
:
self.assertEqual(
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
},
<strong>response.json()</strong>
)
当我在序列化程序中引发验证错误时,输出以 'b' 为前缀(我认为这是字节类型)并且我尝试删除它的所有方法都失败了。
我的序列化器class: class AppInputTypeSerializer(serializers.Serializer): #value = serializers.CharField(required=True, max_length = 100, min_length = 1)
def validate_value(self, data):
length = len(data['value'])
if length < 1 or length > 100:
message = "We are sorry, your reply should be between 1 and 100 characters. Please try again."
data['error'] = message
raise serializers.ValidationError(message)
return data
我的看法: class PresentationLayerView(generics.GenericAPIView): permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
body = request.data
cardType = body['cardType']
if cardType == "TERMS_CONDITIONS":
serializer = AppTandCTypeSerializer(body)
elif cardType == "CHOICE":
serializer = AppChoiceTypeSerializer(body)
elif cardType == "TEXT" or cardType == "INPUT":
serializer = AppTextTypeSerializer(body)
serializer.validate_value(body)
#print(response.text)
return Response({}, status=status.HTTP_200_OK,)
Test_views:
class StartAssessmentViewTests(APITestCase): # Return 如果用户为输入卡类型输入 > 100 个字符,则验证错误
def test_input_type(self):
#response = self.client.get(
# reverse("app-start-assessment")
#)
response = self.client.post(reverse("app-start-assessment"),
json.dumps({"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":4,
"value": "This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":8,
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT"}),
content_type='application/json')
self.assertEqual(
response.content,
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
}, response.json,
)
由于以下前缀,输出是断言失败:
b'{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}'
而不是这个:
{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}
请帮忙。谢谢
Output is an assert failure because of the prefix below.
没有,不是这个原因。原因是因为您正在将二进制字符串与字典进行比较。但即使它是一个简单的字符串,也会失败,因为字符串不是字典,即使它们看起来一样。
response.content
是二进制字符串,而不是字典,因为它最终会导致二进制流作为 HTTP 响应发送。
最好JSON对对象进行解码,然后检查是否与字典等价。您可以使用 response.json()
:
self.assertEqual(
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
},
<strong>response.json()</strong>
)