我有错误:
访问
.save()
后无法调用serializer.data
。如果需要在提交数据库之前访问数据,请检查 ' serializer.validated_data'代替。
即使我不访问serialized.data但序列化.validate_data。
这是我的代码:
views.py
class Login(APIView):
"""
Verify the login given is correct. #FIXME
"""
def post(self, request, format=None):
"""Process the user given
"""
serializer = LoginSerializer(data=request.data)
if not serializer.is_valid():
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST
)
if serializer.verify_user(serializer.validated_data):
serializer.save()
return Response(
serializer.data,
status=status.HTTP_201_CREATED
)
verify_user是我做的自定义功能。如果我不在该函数中使用validated_data,则执行save()。如果不是它给出了那个消息。我不会修改validated_data:
serializers.py
class LoginSerializer(serializers.Serializer):
"""Interface fields for login api
"""
username = serializers.CharField(max_length=50)
password = serializers.CharField(max_length=255)
origin = serializers.CharField(max_length=255, required=False)
def verify_user(self, validated_data):
"""
Try to authenticate a user with given credentials
:return: True on success
"""
password_valid = validated_data['password']
username_valid = validated_data['username']
# TODO: Put in utilities
_password_crp = self.cryp_password(password_valid)
# 1 verify new table
user = User.objects.filter(
username=username_valid,
password=_password_crp
)
if user is None or user.count() == 0:
return False
return True
答案 0 :(得分:6)
问题出在调试模式,因为在调试模式下,即使您没有访问明确但调试器仍然可以访问该对象。