Created
December 14, 2012 03:57
PATCH support for Django REST Framework. The Django Rest Framework (http://django-rest-framework.org/api-guide/serializers.html) does not provide support for the PATCH HTTP method. These two overridden classes will allow you to mix-in support for PATCH. Place these in your Django webapp and import them as needed. I've kept the same DRF file name…
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 rest_framework import mixins | |
from rest_framework.generics import SingleObjectAPIView | |
from yourapp.mixins import PartialUpdateModelMixin | |
class RetrievePartialUpdateDestroyAPIView(PartialUpdateModelMixin, | |
mixins.RetrieveModelMixin, | |
mixins.UpdateModelMixin, | |
mixins.DestroyModelMixin, | |
SingleObjectAPIView): | |
@property | |
def allowed_methods(self): | |
""" | |
Return the list of allowed HTTP methods, uppercased. | |
""" | |
self.http_method_names.append("patch") | |
return [method.upper() for method in self.http_method_names | |
if hasattr(self, method)] | |
def get_serializer(self, instance=None, data=None, files=None, partial=False): | |
""" | |
Return the serializer instance that should be used for validating and | |
deserializing input, and for serializing output. | |
""" | |
serializer_class = self.get_serializer_class() | |
context = self.get_serializer_context() | |
return serializer_class(instance, data=data, files=files, partial=partial, context=context) | |
def patch(self, request, *args, **kwargs): | |
return self.update_partial(request, *args, **kwargs) | |
def get(self, request, *args, **kwargs): | |
return self.retrieve(request, *args, **kwargs) | |
def put(self, request, *args, **kwargs): | |
return self.update(request, *args, **kwargs) | |
def delete(self, request, *args, **kwargs): | |
return self.destroy(request, *args, **kwargs) |
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.http import Http404 | |
from rest_framework import status | |
from rest_framework.response import Response | |
class PartialUpdateModelMixin(object): | |
""" | |
Update a model instance. | |
Should be mixed in with `SingleObjectBaseView`. | |
""" | |
def update_partial(self, request, *args, **kwargs): | |
try: | |
self.object = self.get_object() | |
created = False | |
except Http404: | |
self.object = None | |
created = True | |
serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES, partial=True) | |
if serializer.is_valid(): | |
self.pre_save(serializer.object) | |
self.object = serializer.save() | |
status_code = created and status.HTTP_201_CREATED or status.HTTP_200_OK | |
return Response(serializer.data, status=status_code) | |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
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
# A Sample View to demonstrate how to use the PATCH method | |
from yourapp.generics import RetrievePartialUpdateDestroyAPIView | |
from yourapp.models import Project | |
from yourapp.serializers import ProjectSerializer | |
class ProjectDetail(RetrievePartialUpdateDestroyAPIView): | |
model = Project | |
permission_classes = (permissions.AllowAny,) | |
serializer_class = ProjectSerializer | |
# your serializer class should not need to change. |
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
Some curl commands to help test: | |
Create: | |
curl -XPOST -H "Content-type: application/json" -d '{"project_name":"My Project", "project_description":"The original description"}' http://localhost:8000/projects/ | |
Update: | |
curl -XPATCH -H "Content-type: application/json" -d '{"project_name":"A new name"}' http://localhost:8000/project/1/ | |
OR | |
curl -XPUT -H "Content-type:application/json" -d '{"project_name":"A new name", "project_description":"The original description"}' http://localhost:8000/project/1/ | |
Delete: | |
curl -XDELETE http://localhost:8000/project/1/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for posting this! According to the pending IETF standard, however, this is a non-standard way of using PATCH. Still, quite useful to see how it might be implemented.