I am trying to use a ListSerializer so that I can create/de-serialize multiple objects in a list on a POST. I followed the guide at https://www.django-rest-framework.org/api-guide/serializers/#listserializer but seem to be running into this error when i visit the endpoint.
assert self.child is not None, '``child`` is a required argument.'
python3.7/site-packages/rest_framework/serializers.py in __init__, line 592
My serializers are as follows:
class PredictionListSerializer(serializers.ListSerializer):
def update(self, instance, validated_data):
pass
def create(self, validated_data):
predictions = [Prediction(**item) for item in validated_data]
return Prediction.objects.bulk_create(predictions)
class NestedPredictionSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only=True)
# Nested Serializer
data = DataSerializer()
class Meta:
model = Prediction
list_serializer_class = PredictionListSerializer
fields = ('id', 'specimen_id', 'data' 'date_added',)
extra_kwargs = {
'slug': {'validators': []},
}
datatables_always_serialize = ('id',)
The assertion error is being thrown in the initialization of the ListSerializer, however, the serializer is being initialized in a ViewSet like so.
class BulkPredictionViewSet(viewsets.ModelViewSet):
queryset = Prediction.objects.all()
serializer_class = PredictionListSerializer
Anyone familiar with this issue? I am wondering if my lack o control around initialization of the serializer (because I am using a ViewSet) is affecting this. If I try to pass the NestedPredictionSerializer into the ViewSet I get "Invalid data. Expected a dictionary, but got list."
EDIT:
I was able to overwrite the get_serializer
method in my ViewSet to set many=True and pass the serializer as my NestedPredictionSerializer which recognizes the list on a POST. Howeber, on a get now I receive the error When a serializer is passed a ``data`` keyword argument you must call ``.is_valid()`` before attempting to access the serialized ``.data`` representation.
You should either call ``.is_valid()`` first, or access ``.initial_data`` instead.