class CreateModelMixin

from rest_framework.mixins import CreateModelMixin
Documentation Source code
    Create a model instance.
    Should be mixed in with any `BaseView`.
    

Ancestors (MRO)

  1. CreateModelMixin

Methods used but not implemented in this class

  • get_serializer
Expand Collapse

Methods

def create(self, request, *args, **kwargs): CreateModelMixin

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.DATA, files=request.FILES)

        if serializer.is_valid():
            self.pre_save(serializer.object)
            self.object = serializer.save()
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED,
                            headers=headers)

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

def get_success_headers(self, data): CreateModelMixin

    def get_success_headers(self, data):
        try:
            return {'Location': data['url']}
        except (TypeError, KeyError):
            return {}

def pre_save(self, obj): CreateModelMixin

    def pre_save(self, obj):
        pass