*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:*
{
"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)


*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" : "[email protected]"
                     }
]
}]


*Can anyone please suggest what changes could be done in the serializer so 
that I could overcome the issue here.?*

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7e2e6920-3ef5-488a-bd5f-59d103de9977%40googlegroups.com.

Reply via email to