Django REST - separating valid data from non-valid and serializing the former with many=True -
i using django rest framework come rest api app. in 1 of views, trying use many=true
when initializing serializer object in order bulk_insert multiple rows @ once. problem if 1 of records in dataset invalid, serializer's is_valid()
method return false
, rejecting entire dataset. whereas desired behavior inserting valid records , ignoring invalid ones. have succeed in achieving using following code, have terrible feeling junk code , rest framework has native way this.
my code below (that consider junk code :)):
serializers.py
class myserializer(serializers.modelserializer): class meta: model = calendareventattendee fields = '__all__'
view.py
def my_view(request): validated_data = [] # separate valid data invalid record in request.data: if myserializer(data = record).is_valid(): validated_data.append(record) # bulk_insert valid data serializer = myserializer(data=validated_data, many=true) if serializer.is_valid(): serializer.save()
can suggest better approach ?
Comments
Post a Comment