本文介绍了django序列化器中的类型不匹配以进行请求解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了以下序列化程序用于JSON数据的请求解析。但是,在执行该操作时,出现了意外错误。
I had created the following serializers for request parsing of JSON data. However, while performing the operation, I get an unexpected error.
class A(serializers.ModelSerializer):
class Meta:
model = CName
fields = ('id','contact','email')
read_only_fields=('contact',)
class B(serializers.ModelSerializer):
class Meta:
model = PName
fields = ('id','contact','number')
read_only_fields=('contact',)
class C(serializers.ModelSerializer):
contact_number = A(many=True)
contact_email = B(many=True)
class Meta:
model = Contact
fields = ('id','name','contact_number','contact_email')
def create(self,validated_data):
contact_number=validated_data.pop('contact_number')
contact_email =validated_data.pop('contact_email')
instance = Contact.objects.create(**validated_data)
for number in contact_number:
PName.objects.create(contact=instance,**number)
for email in contact_email:
CName.objects.create(contact=instance,**email)
return instance
def update(self, instance, validated_data):
contact_number=validated_data.pop('contact_number')
contact_email =validated_data.pop('contact_name')
Contact.objects.filter(id=instance.id).update(**validated_data)
number_to_keep=[]
email_to_keep=[]
for number in contact_number:
if number.get('id'):
ph_id = number.pop('id')
PName.objects.filter(id=ph_id).update(**number)
number_to_keep.append(ph_id)
else:
ph=PName.objects.create(contact=instance,**number)
number_to_keep.append(ph.id)
for email in contact_email:
if email.get('id'):
em_id = email.pop('id')
CName.objects.filter(id=em_id).update(**email)
email_to_keep.append(em_id)
else:
em = CName.objects.create(contact=instance,**email)
email_to_keep.append(em.id)
instance.contact_number.exclude(id__in=number_to_keep).delete()
instance.contact_email.exclude(id__in=email_to_keep).delete()
return instance
我有一个json格式,其中我以以下格式传递请求数据(仅字符串):
{
"contact_number": "9999999999",
"contact_email":"[email protected]"
}
使用以下代码调用序列化程序时:
While calling up the serializer using the following code:
contact_details = Contact.objects.get(rest = rest)
contact_serializer = ContactSerializer(contact_details,data=request.data)
我收到了以下答复:
{
"contact_number": {
"non_field_errors": [
"Expected a list of items but got type \"unicode\"."
]
},
"contact_email": {
"non_field_errors": [
"Expected a list of items but got type \"unicode\"."
]
}
}
注意:不能采用以下格式的JSON数据格式:
{
"contact_number" : [
{ "number" : "9999999999"
}
],
"contact_email" : [
{ "email" : "[email protected]"
}
]
}
任何人都可以提出更改建议在序列化程序中完成,这样我就可以在这里解决问题。
Can anyone please suggest what changes could be done in the serializer so that I could overcome the issue here.
推荐答案
class A(serializers.ModelSerializer):
class Meta:
model = CName
fields = ('id','contact','email')
read_only_fields=('contact',)
def save(self, **kwargs):
[obj.save(**kwargs) for obj in self]
在保存时使用自定义保存方法。
Use custom save method while saving.
这篇关于django序列化器中的类型不匹配以进行请求解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!