问题
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
I have a json-format where I am passing the request data in the format(I will have strings only):
{
"contact_number": "9999999999",
"contact_email":"timsh@hotmail.com"
}
While calling up the serializer using the following code:
contact_details = Contact.objects.get(rest = rest)
contact_serializer = ContactSerializer(contact_details,data=request.data)
I received the response as below:
{
"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\"."
]
}
}
Note: I cannot have the data format of JSON in the below format:
{
"contact_number" : [
{ "number" : "9999999999"
}
],
"contact_email" : [
{ "email" : "tim@hotmail.com"
}
]
}
Can anyone please suggest what changes could be done in the serializer so that I could overcome the issue here.
回答1:
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.
来源:https://stackoverflow.com/questions/60853584/type-mismatch-in-django-serializer-for-request-parsing