Skip to content

Instantly share code, notes, and snippets.

@prudnikov
Created December 17, 2019 09:55
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prudnikov/3a968a1ee1cf9b02730cc40bc1d3d9f2 to your computer and use it in GitHub Desktop.
Save prudnikov/3a968a1ee1cf9b02730cc40bc1d3d9f2 to your computer and use it in GitHub Desktop.
Django Rest Framework Atomic ViewSet and Mixin.
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