Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PUT-as-create mixin class for Django REST framework.
class AllowPUTAsCreateMixin(object):
The following mixin class may be used in order to support PUT-as-create
behavior for incoming requests.
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object_or_none()
serializer = self.get_serializer(instance,, partial=partial)
if instance is None:
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
lookup_value = self.kwargs[lookup_url_kwarg]
extra_kwargs = {self.lookup_field: lookup_value}**extra_kwargs)
return Response(, status=status.HTTP_201_CREATED)
return Response(
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
def get_object_or_none(self):
return self.get_object()
except Http404:
if self.request.method == 'PUT':
# For PUT-as-create operation, we need to ensure that we have
# relevant permissions, as if this was a POST request. This
# will either raise a PermissionDenied exception, or simply
# return None.
self.check_permissions(clone_request(self.request, 'POST'))
# PATCH requests where the object does not exist should still
# return a 404 response.
Copy link

Are there any plans to have something like this included in DRF by default?

Copy link


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment