我的命令:
python manage.py shell
from user.models import UserInfo
from user.serializers import UserInfoSerializer
import io
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
u=UserInfo.objects.all()[0]
s=UserInfoSerializer(u)
j=JSONRenderer().render(s.data)
o=io.BytesIO(j)
d=JSONParser().parse(o)
s1=UserInfoSerializer(data=d)
s1.is_valid()
但是这里的问题是s1.is_valid()始终是False,我无法将s1保存为序列化器。 我收到此错误:
>>> d
{'username': 'user001', 'password': 'pass001', 'email': 'user001@example.com', 'contact': 9876543210}
>>> s001=UserInfoSerializer(data=d)
>>> s001.is_valid()
False
>>> s001.validated_data()
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'dict' object is not callable
>>> s001.validated_data
{}
>>> s001.data
{'username': 'user001', 'password': 'pass001', 'email': 'user001@example.com', 'contact': 9876543210}
>>> s001.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/imharjyotbagga/PycharmProjects/DRF/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 182, in save
'You cannot call `.save()` on a serializer with invalid data.'
AssertionError: You cannot call `.save()` on a serializer with invalid data.
>>>
那我该怎么办!!
答案 0 :(得分:0)
您甚至需要显示.errors
属性才能尝试诊断问题。我的盲目猜测是您弄乱了密码字段。但是,除非您生成序列化程序,模型的代码,并且序列化程序的.errors
也会很有帮助,否则您将无法得到任何明确的答案。