Month: May 2021

Django Authentication With Custom User Model

By default, Django uses a built-in User model for authentication. If you’d like a basic tutorial on how to implement login, log out, sign up you can see the Django Authentication SignUpView -LoginView-LogoutView

However, the Django documentation highly recommends using a custom user model for a real-world project. Because this provides far more flexibility. In todays’ article, we will see how to create a custom user model for Django projects.

The default User model in Django uses a username to uniquely identify a user during authentication. We can create a custom Django user model by either subclassing AbstractUser or AbstractBaseUser. The difference is AbstractUser uses the existing fields of the User model and just allows to remove the username field.

On the contrary, AbstractBaseUser is used if one wants to start from scratch by creating completely a new User model. Here we will use  AbstractBaseUser

Firstly, we have created a Django project called ‘CUSTOM_AUTHENTICATION’ and an app named ‘custom_auth’. Now we are all set to start our work. So, without further ado let’s create our CustomUser model first.

Read More

CRUD (Create Retrieve Update Delete) in DRF.

CRUD is an approach to build a Django web application. CRUD stands for Create Retrieve Update Delete.

In simple words CRUD means performing Create, Retrieve, Update and Delete operations on a table in a database.

Django REST provides some generic view classes for doing these operations easily and there is an HTTP method handler assigned with each of them.

CRUDHTTP VerbUsage
CreatePOST(Creates a new instance of a model)
RetrieveGET(Brings the value of an instance)
UpdatePUT/PATCH(Updates the value of an instance)
DeleteDELETE(Deletes an instance)

In this article we will implement each of the operations in our project. First, we will create one API for each operation. Then we will see how to do multiple operations with only one API endpoint.

Read More

Django REST Framework | Introduction

Django REST Framework is a library which works on top of Django. It can be called a Microframework. In django we return a HttpResponse object from views. But in DRF we return JSON object. DRF helps to create an  RESTful API endpoint. “Browsable API” was stated as the key factor in picking Django REST framework.

What is an API?

If you are totally new to development, you might have never heard about API. It’s totally okey. But did you ever think how data travel from one application to another?

API does that. It stands for Application Programming Interface. APIs transport data from one app to another. So, API is a messenger that takes request and returns the response or a software intermediary that allows two applications to talk to each other.

Think of a website that compares mobile phones of different brands. The site doesn’t have the information of each model of each brand in its database. Then how do they compare? Each time a user selects a mobile model, the backend of PhoneCompare site sends a request through the API to its manufacturer’s website to send information. If the request is valid the API comes with the response.

This is how it retrieves data and then compares them using it’s own mechanism.

Read More

Powered by WordPress & Theme by Anders Norén