From d845ba32b33c8a2d760b458b7e68b109777e2f9e Mon Sep 17 00:00:00 2001 From: Ashton Hudson Date: Sun, 19 Jan 2020 20:32:02 +0200 Subject: [PATCH] feat #6084: Adding mixin that allows for many object creations. The original issue #6084 has an issue where the browsable api cannot create multiple objects, but sometimes being able to creat more than object object is desired. This commit proposes a mixin that allows for one or more objects to be created, which allows normal browsable api operation, and the use case of creating more than one instance. --- rest_framework/mixins.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 7fa8947cb..d2666143f 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -30,6 +30,19 @@ class CreateModelMixin: return {} +class CreateOneOrMoreModelMixin(CreateModelMixin): + """ + Create one or more model instances. + """ + + def get_serializer(self, *args, **kwargs): + if self.request.method == 'POST': + kwargs['many'] = isinstance(self.request.data, list) or kwargs.get( + 'many', False + ) + return super().get_serializer(*args, **kwargs) + + class ListModelMixin: """ List a queryset.