Created
December 17, 2019 09:55
-
-
Save prudnikov/3a968a1ee1cf9b02730cc40bc1d3d9f2 to your computer and use it in GitHub Desktop.
Django Rest Framework Atomic ViewSet and Mixin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import transaction | |
from rest_framework import mixins | |
from rest_framework.viewsets import GenericViewSet | |
__all__ = ['AtomicCreateModelMixin', 'AtomicUpdateModelMixin', 'AtomicDestroyModelMixin', | |
'AtomicModelViewSetMixin', 'AtomicModelViewSet'] | |
class AtomicCreateModelMixin(mixins.CreateModelMixin): | |
@transaction.atomic | |
def create(self, request, *args, **kwargs): | |
return super().create(request, *args, **kwargs) | |
class AtomicUpdateModelMixin(mixins.UpdateModelMixin): | |
@transaction.atomic | |
def update(self, request, *args, **kwargs): | |
return super().update(request, *args, **kwargs) | |
class AtomicDestroyModelMixin(mixins.DestroyModelMixin): | |
@transaction.atomic | |
def destroy(self, request, *args, **kwargs): | |
return super().destroy(request, *args, **kwargs) | |
class AtomicModelViewSetMixin(AtomicUpdateModelMixin, AtomicCreateModelMixin, AtomicDestroyModelMixin): | |
pass | |
class AtomicModelViewSet(AtomicCreateModelMixin, | |
mixins.RetrieveModelMixin, | |
AtomicUpdateModelMixin, | |
AtomicDestroyModelMixin, | |
mixins.ListModelMixin, | |
GenericViewSet): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment