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?
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.
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.
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.
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.
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 {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With