Django REST Framework prevent multiple validation queries in batch create
<p>When trying to do bulk inserts using Django/Django Rest Framework 3, the validation step is causing <code>n * related_model_fields</code> queries, which is causing a lot of unnecessary latency. </p>

<p>In the example below, <code>Thing</code> has two related fields, one being user (added in the view), and the other being the <code>pk</code> of another model in a foreign key related field. Each item in the batch is individually being validated, and that validation includes look ups on the <code>User</code> model, and the other related model, resulting in 2 queries per item in the batch just for validation.</p>

<p>Is there any way to override this behavior to do a "batch" validation of the data? Or can I override the validation behavior to validate against a pre-queried set of values to prevent multiple database round-trips?</p>

<pre><code>class ThingView(APIView):
    def post(self, request):
        # Add user to each record
        user_id = self.request.user.id
        map(lambda rec: rec.update(user=user_id), request.data)

        serializer = ThingSerializer(data=request.data, many=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class ThingListSerializer(serializers.ListSerializer):
    def create(self, validated_data):
        things = [Thing(**item) for item in validated_data]
        return Thing.objects.bulk_create(things)

class ThingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thing
        list_serializer_class = ThingListSerializer
</code></pre>

<p>You'll likely want to roll your own validators and replace the default's one. For example, on the <code>User</code> you'll have a UniqueValidator that will query the DB for existing <code>username</code>. You could remove it and deal with that constraint explicitly by yourself - done that for some batch import.</p>

python django django-rest-framework