rest_framework insert multible json object

346 views
Skip to first unread message

Orçun ULUTAŞ

unread,
Feb 10, 2021, 7:53:57 PM2/10/21
to Django users
Hi All,
I need a help about rest_framework because I can't figure out the problem.
need to insert bulk json object via django rest_framework however, I get an error message when send data to django rest page ;
{"name":"m1","duration":120,"rating":4.7}, 
{"name":"m2","duration":120,"rating":4.5} 
]

typed following line ;

{ "non_field_errors": [ "Invalid data. Expected a dictionary, but got list." ] }

my serializer.py file include following code block;

class DrfSerializerV2(serializers.ModelSerializer): def __init__(self,*args,**kwargs):many=kwargs.pop('many',True)super(DrfSerializerV2,self).__init__(many=True,*args,**kwargs) class Meta: model = DrfData fields=['id','name','duration','rating','typ']
serializer.png

and my views.py file ;

class ActionViewSet(viewsets.ModelViewSet): queryset = DrfData.objects.filter(typ="action") serializer_class = serializers.DrfSerializerV2
viewes.png

finally send json file via post;

[ {"name":"m1","duration":120,"rating":4.7}, {"name":"m2","duration":120,"rating":4.5} ]

and django typed following error message;

{ "non_field_errors": [ "Invalid data. Expected a dictionary, but got list." ] }

however, I can't handle this error message. if I send sperataly, there is no problem.

but, I can't resolve this error for multiple json insert


Thanks

Samuel Nogueira

unread,
Feb 11, 2021, 10:10:34 PM2/11/21
to django...@googlegroups.com
You can create a custom Create() function in your viewset to handle your list of dicts. Inside that function you can iterate our list to get each dict and pass the dict to the serializer
--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/182af5ab-a3de-4abe-83e4-ff0e2571f98an%40googlegroups.com.
viewes.png
serializer.png
Reply all
Reply to author
Forward
0 new messages