Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 431
GluePear Avatar asked Nov 15 '18 12:11

GluePear


People also ask

What is the Django REST framework?

The Django REST Framework (DRF) is a package built on top of Django to create web APIs. One of the most remarkable features of Django is its Object Relational Mapper (ORM) which facilitates interaction with the database in a Pythonic way.

What is an API in Django?

In this article, we will know about APIs, Django REST APIs, the HTTP methods, and then in the last, create our very own first REST API application. What is an API? API is short for Application Programming Interface and it allows you to interface with other applications and pull/process/push data and values when required.

How to use JSON text in Django REST API?

JSON text looks just like the python dictionary. Now to use Django REST API, we have an entire framework called the Django Rest framework. We need to install that into our environment with the use of the pip command, just like how we installed Django. That’s it; now it’s installed.

How to create a model of an item in Django?

Create a new Django app in our project with the name “itemsapp” using the way we learned in Django Hello World App 2. Create a model itemModel having the list of items in it. In models.py, create a model having all the necessary information needed for an item as shown. Now that our model is ready, we will learn about about a new thing called 3.


1 Answers

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 {}
like image 122
Pythonista Avatar answered Sep 20 '22 09:09

Pythonista