Search code examples
pythondjangodjango-rest-auth

What is the difference between the create and perform_create methods in Django rest-auth


I'm using the Django rest-auth package. I have a class that extends rest-auth's RegisterView, and which contains two methods, create and perform_create. What is the difference between these two methods?


Solution

  • perform_create is called within the create method to call the serializer for creation once it's known the serialization is valid. Specifically, serializer.save()

    Code from the source - when in doubt check it:

    class CreateModelMixin(object):
        """
        Create a model instance.
        """
        def create(self, request, *args, **kwargs):
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
    
        def perform_create(self, serializer):
            serializer.save()
    
        def get_success_headers(self, data):
            try:
                return {'Location': str(data[api_settings.URL_FIELD_NAME])}
            except (TypeError, KeyError):
                return {}