2021-03-30

how can insert multiple record using

I'm working on a small project Django Rest Framework, I already create add contact function as you can see in my create function. now I'm working on bulk import, but when I submit my data as a list not as a dict I get an error message :

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

this is my code to add a contact,

class ContactView(ListModelMixin, viewsets.GenericViewSet):
    queryset = Contact.objects.all()
    serializer_class = ContactSerializer

    def create(self, request):
        serializeObject = ContactSerializer(data = request.data)
        if serializeObject.is_valid():
            serializeObject.save()
            contactObject = Contact.objects.all()
            contactSerializer = ContactSerializer(contactObject, many=True)
            return Response(contactSerializer.data, status = status.HTTP_201_CREATED)
        return Response(serializeObject.errors, status.HTTP_400_BAD_REQUEST)

Now i would like to create another function, for bulk create, since i have a list

This is my header data structure :

[{"Greeting":"amine","first_name":"alain","last_name":"amine","title":"ricardo","language":"ab@xyz.com","email":43822510594,"phone_1":43822510594,"phone_2":"not yet","mobile":43822510594,"fax":"not yet","internal_id":"xname"},{"Greeting":"bill","first_name":"microsoft","last_name":"bill","title":"microsoft","language":"bill@microsoft.com","email":652565455,"phone_1":652565455,"phone_2":"new york","mobile":652565455,"fax":"new york","internal_id":"microsoft"},{"Greeting":"john","first_name":"Yoyo","last_name":"Ruth","title":"xnameagain","language":"rh@xyz.com","email":5465559852,"phone_1":5465559852,"phone_2":"Vancouver","mobile":5465559852,"fax":"Vancouver","internal_id":"yname"}]

This is my serializer:

class ContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = Contact
        fields = "__all__"


from Recent Questions - Stack Overflow https://ift.tt/3uiEWrh
https://ift.tt/eA8V8J

No comments:

Post a Comment