class UpdateModelMixin

from rest_framework.mixins import UpdateModelMixin
Documentation Source code
    Update a model instance.
    Should be mixed in with `SingleObjectAPIView`.

Ancestors (MRO)

  1. UpdateModelMixin

Methods used but not implemented in this class

  • get_slug_field
  • post_save
  • get_serializer
  • get_object
  • check_permissions
Expand Collapse


def pre_save(self, obj): UpdateModelMixin

        Set any attributes on the object that are implicit in the request.
    def pre_save(self, obj):
        Set any attributes on the object that are implicit in the request.
        # pk and/or slug attributes are implicit in the URL.
        pk = self.kwargs.get(self.pk_url_kwarg, None)
        slug = self.kwargs.get(self.slug_url_kwarg, None)
        slug_field = slug and self.get_slug_field() or None

        if pk:
            setattr(obj, 'pk', pk)

        if slug:
            setattr(obj, slug_field, slug)

        # Ensure we clean the attributes so that we don't eg return integer
        # pk using a string representation, as provided by the url conf kwarg.
        if hasattr(obj, 'full_clean'):
            exclude = _get_validation_exclusions(obj, pk, slug_field)

def update(self, request, *args, **kwargs): UpdateModelMixin

    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        self.object = None
            self.object = self.get_object()
        except Http404:
            # If this is a PUT-as-create operation, we need to ensure that
            # we have relevant permissions, as if this was a POST request.
            self.check_permissions(clone_request(request, 'POST'))
            created = True
            save_kwargs = {'force_insert': True}
            success_status_code = status.HTTP_201_CREATED
            created = False
            save_kwargs = {'force_update': True}
            success_status_code = status.HTTP_200_OK

        serializer = self.get_serializer(self.object, data=request.DATA,
                                         files=request.FILES, partial=partial)

        if serializer.is_valid():
            self.object =**save_kwargs)
            self.post_save(self.object, created=created)
            return Response(, status=success_status_code)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)