Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Batch Create

I'm trying to make an api where you can create multiple objects with a single Post request using django rest framework. However, my serializer is providing an error which I don't understand. First off here's the a skeleton of my code

My Serializer:

class MyModelSerializer(serializers.ModelSerializer):
# override the constructor to always use the many flag
def __init__(self, *args, **kwargs):
    many = kwargs.pop('many', True)
    super(MyModelSerializer, self).__init__(many=many, *args, **kwargs)

class Meta:
    model = MyModel
    fields = ('field1', 'field2')

My List View

class MyModelListView(generics.ListCreateAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    def post(self, request, *args, **kwargs):
        if request.DATA['batch']:
            json = request.DATA['batchData']
            stream = StringIO(json)
            data = JSONParser().parse(stream)
            request._data = data
        return super(CharacterDatumList, self).post(request, *args, **kwargs)

My ajax call

$.ajax({
    type: "POST",
    url: '/characterDatum/',
    data: {
        'batch' : true,
        'batchData' : '[{"field1": "x", "field2": "y", },{"field1": "a", "field2": "b"}]'
    },
    success: function(response) {
        console.log('success');
    },
    datatype: 'json'
});

However, in the django rest framework I get a serialization validation error at the line below

class CreateModelMixin(object):
    """
    Create a model instance.
    """
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.DATA, files=request.FILES)
        if serializer.is_valid(): # returns not valid

Does anyone know why this might be the case?

like image 823
Peter Tseng Avatar asked Jan 29 '14 18:01

Peter Tseng


1 Answers

I found it! The serializer was not actually using the many option so I had to override the get_serializer method in MyModelListView

def get_serializer(self, instance=None, data=None,
                        files=None, many=True, partial=False):
        return super(MyModelListView, self).get_serializer(instance, data, files, many, partial)
like image 112
Peter Tseng Avatar answered Oct 23 '22 16:10

Peter Tseng