django rest framework 根据自定义 class 反序列化对象
django rest framework deserialize object based on custom class
我想反序列化这个 json:
json1 = {
"age" : "22",
"name" : "Bob",
"lastname" : "Andrew",
"contactList" : [
{ "friend" : "Alice"},
{ "friend" : "John"}
]}
我创建了以下 类(我不想创建任何模型,因为我不想将它们保存在数据库中):
class Friend(object):
def __init__(self, friend):
self.friend = friend
class Person(object):
def __init__(self, age , name , lastname, contactList):
self.age=age
self.name = name
self.lastname = lastname
self.contactList= [] #possible error here
和以下序列化器:
class FriendSeriliazer(serializers.Serializer):
friend = serializers.CharField()
def create(self, validated_data):
return Friend(**validated_data)
class PersonSerializer(serializers.Serializer):
age = serializers.CharField()
name = serializers.CharField()
lastname = serializers.CharField()
contactList = FriendSerializer(many=True)
def create(self, validated_data):
print validated_data
simple = Person(**validated_data)
contactList = self.validated_data.pop('contactList')
for friend in contactList:
serializer = FriendSerializert(data=friend)
if serializer.is_valid(raise_exception=True):
person.contactList.append(serializer.save())
return person
POST方法从视图:
parsedData = PersonSerializer(data=request.data)
person = parsedData.save()
print person.contactList[0].friend #<-- nested object should be present
return Response(request.data, status=status.HTTP_200_OK)
我为此使用了 drf documentation
问题是,虽然这可行,但我需要反序列化一个更复杂的 json 并且在 create 函数中迭代内部对象不会削减它。有没有更自动化的方法?
在你的情况下,你不想保存在数据库中,而是保存在对象中,并且 Django-Rest-Framework 默认不支持可写的嵌套序列化程序,然后没有比您正在做的更好的解决方案了。你必须自己实现这个过程。
By default nested serializers are read-only. If you want to support
write-operations to a nested serializer field you'll need to create
create()
and/or update()
methods in order to explicitly specify how
the child relationships should be saved.
阅读 Django-Rest-Framework 文档以获取更多信息:Writable nested relationships
我想反序列化这个 json:
json1 = {
"age" : "22",
"name" : "Bob",
"lastname" : "Andrew",
"contactList" : [
{ "friend" : "Alice"},
{ "friend" : "John"}
]}
我创建了以下 类(我不想创建任何模型,因为我不想将它们保存在数据库中):
class Friend(object):
def __init__(self, friend):
self.friend = friend
class Person(object):
def __init__(self, age , name , lastname, contactList):
self.age=age
self.name = name
self.lastname = lastname
self.contactList= [] #possible error here
和以下序列化器:
class FriendSeriliazer(serializers.Serializer):
friend = serializers.CharField()
def create(self, validated_data):
return Friend(**validated_data)
class PersonSerializer(serializers.Serializer):
age = serializers.CharField()
name = serializers.CharField()
lastname = serializers.CharField()
contactList = FriendSerializer(many=True)
def create(self, validated_data):
print validated_data
simple = Person(**validated_data)
contactList = self.validated_data.pop('contactList')
for friend in contactList:
serializer = FriendSerializert(data=friend)
if serializer.is_valid(raise_exception=True):
person.contactList.append(serializer.save())
return person
POST方法从视图:
parsedData = PersonSerializer(data=request.data)
person = parsedData.save()
print person.contactList[0].friend #<-- nested object should be present
return Response(request.data, status=status.HTTP_200_OK)
我为此使用了 drf documentation
问题是,虽然这可行,但我需要反序列化一个更复杂的 json 并且在 create 函数中迭代内部对象不会削减它。有没有更自动化的方法?
在你的情况下,你不想保存在数据库中,而是保存在对象中,并且 Django-Rest-Framework 默认不支持可写的嵌套序列化程序,然后没有比您正在做的更好的解决方案了。你必须自己实现这个过程。
By default nested serializers are read-only. If you want to support write-operations to a nested serializer field you'll need to create
create()
and/orupdate()
methods in order to explicitly specify how the child relationships should be saved.
阅读 Django-Rest-Framework 文档以获取更多信息:Writable nested relationships