在 Django rest 框架中反序列化后获取 class 个实例
Getting the class instances after deserialization in Django rest framework
我正在尝试从给定的序列化中创建 Userr
对象。在 Django REST Framework 文档中,他们提到了必须 return 实例的 save() 函数,但事实并非如此。
class Userr(object):
def __init__(self, email, name):
self.email = email
self.name = name
class UserrSerializer(serializers.Serializer):
email = serializers.EmailField()
name = serializers.CharField()
def create(self, validated_data):
return Userr(**validated_data)
u_serializer = UserrSerializer(data={'email': bla.bla@ex.org', 'name': bla'})
print (u_serializer.is_valid())
user = u_serializer.save()
我希望 user
变量用 class Userr
的对象初始化。相反,我看到以下错误:
Traceback (most recent call last):
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/hbp_knowledge_graph/ontology/tests/test_dataset_json_deserializer.py", line 73, in testSerializers
u_serializer.save()
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/rest_framework/serializers.py", line 598, in save
self.save_object(self.object, **kwargs)
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/rest_framework/serializers.py", line 580, in save_object
obj.save(**kwargs)
AttributeError: 'dict' object has no attribute 'save'
我做错了什么?
感谢 mariodev,我得到了它。我使用 rest_framework 2.3 版并尝试在其上编写 运行 3.0 代码。
对于 3.0,class 应该是这样的:
class UserrSerializer(serializers.Serializer):
email = serializers.EmailField()
name = serializers.CharField()
def restore_object(self, attrs, instance=None):
if instance:
# Update existing instance
instance.email = attrs.get('email', instance.email)
instance.email = attrs.get('name', instance.name)
return instance
return Userr(**attrs)
我正在尝试从给定的序列化中创建 Userr
对象。在 Django REST Framework 文档中,他们提到了必须 return 实例的 save() 函数,但事实并非如此。
class Userr(object):
def __init__(self, email, name):
self.email = email
self.name = name
class UserrSerializer(serializers.Serializer):
email = serializers.EmailField()
name = serializers.CharField()
def create(self, validated_data):
return Userr(**validated_data)
u_serializer = UserrSerializer(data={'email': bla.bla@ex.org', 'name': bla'})
print (u_serializer.is_valid())
user = u_serializer.save()
我希望 user
变量用 class Userr
的对象初始化。相反,我看到以下错误:
Traceback (most recent call last):
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/hbp_knowledge_graph/ontology/tests/test_dataset_json_deserializer.py", line 73, in testSerializers
u_serializer.save()
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/rest_framework/serializers.py", line 598, in save
self.save_object(self.object, **kwargs)
File "/Users/katkov/PycharmProjects/hbp-knowledge-graph/platform_venv/lib/python2.7/site-packages/rest_framework/serializers.py", line 580, in save_object
obj.save(**kwargs)
AttributeError: 'dict' object has no attribute 'save'
我做错了什么?
感谢 mariodev,我得到了它。我使用 rest_framework 2.3 版并尝试在其上编写 运行 3.0 代码。 对于 3.0,class 应该是这样的:
class UserrSerializer(serializers.Serializer):
email = serializers.EmailField()
name = serializers.CharField()
def restore_object(self, attrs, instance=None):
if instance:
# Update existing instance
instance.email = attrs.get('email', instance.email)
instance.email = attrs.get('name', instance.name)
return instance
return Userr(**attrs)