Category: DRF

How to use ViewSets and Routers | Django REST-Framework

Django REST Framework allows us to combine the logic for a set of related views in a single view class. They are called Viewsets. ViewSets work exactly same as Generic Views. The only difference is using ViewSet we don’t have to create separate views for getting list of objects and detail of one object. We do not need to configure the urls with ViewSets. Routers generates urls automatically and binds methods for different request method types of ViewSet.

Why we should use ViewSets & Routers.

In larger projects multiple endpoints (URLs) can be difficult to maintain . Besides, same lines of code are repeated in the views. These problems can be solved using ViewSets & Routers.

Advantages of using ViewSet.

  • Can speed up API development
  • Additional layer of abstraction
  • A single viewset can replace multiple views
  • Repeated logic can be combined into a single class
  • Combined logic for a set of related views in a single class
  • Write less code – Promotes DRY (Don’t Repeat Yourself)

Firstly, we will see usage of Viewset class and then ModelViewset class.

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

Serializer and ModelSerializer | Django REST Framework

What is a Serializer?

A Serializer takes a data that exists on the server, parses data from complex types into a format (JSON or XML) that can be read by other technologies.

Or,

A serializer is a framework that allows complex data such as ‘querysets’ and model instances to be converted to native Python data types. Then, we can easily render these into JSON, XML, or other content types.

There Two types of Serializers in Django.

  • serializers.Serializer &
  • serializers.ModelSerializer

The Serializers and ModelSerializers work in a way similar to Forms and ModelForms. Unlike forms, they are not constrained to dealing with HTML output and form encoded input.

We will discuss both types.

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