Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk create #304

Open
TJHeeringa opened this issue Feb 15, 2021 · 0 comments
Open

Bulk create #304

TJHeeringa opened this issue Feb 15, 2021 · 0 comments

Comments

@TJHeeringa
Copy link

I was looking for a bulk create and a bulk delete option for DRF. I see that in the code there is a bulk create and a bulk update. Would it be possible to add a bulk create too, that uses the validation from a serializer?

I am not sure how this would look like, but I guess it would look something like

class ListCreateModelMixin(BulkOperationBaseMixin):
    def create(self, request, *args, **kwargs):
        if self.is_object_operation():
            return super().create(request, *args, **kwargs)
        else:
            return self.create_bulk(request, *args, **kwargs)

    def create_bulk(self, request, *args, **kwargs):
        is_valid, errors = self.is_valid_bulk_operation()
        if is_valid:
            queryset = self.filter_queryset(self.get_queryset())
            self.pre_create_bulk(queryset)
            serializer = self.get_serializer(data=request.data, many=True)
            serializer.is_valid(raise_exception=True)
            self.perform_bulk_create(serializer)
            self.post_create_bulk(queryset) 
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            return Response(errors, status=status.HTTP_400_BAD_REQUEST)

    def perform_bulk_create(self, serializer):
        serializer.save()

    def pre_create_bulk(self, queryset):
        """
        Placeholder method for calling before creating an queryset.
        """
        pass

    def post_create_bulk(self, queryset):
        """
        Placeholder method for calling after creating an queryset.
        """
        pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant