Django REST API - CRUD Tutorial
Tags: django, python, webdev, tutorial
Published: 2020-10-12 22:18:31

Django REST framework is a tool used to make it easier to build web APIs. This framework offers secure and advanced features such as serializers which are used to pass data.

The data passed will be in JSON or XML form which allows for flexibility since it lets us implement the frontend using other programming languages such as React, Vue.js, or Angular just to mention a few.

Let’s start by installing Django REST framework. Go to the terminal window and issue the following command.


![Screen Shot 2020-10-12 at 4.48.37 PM](https://dev-to-uploads.s3.amazonaws.com/i/9gq7ligp76cfynzeaz2n.png)
 

Notice that I have created and activated a virtual environment and I’m issuing the command from the folder which will host all my files for this project. 

If you want to know more about setting up your database with PostgreSQL, create a virtual environment, and more, check out this tutorial.

[Django Takeoff Series - Chapter 1 - Setup ](https://dev.to/balt1794/chapter-1-django-web-app-setup-django-3-2-1-takeoff-series-3550)

Add Django REST framework to the installed apps section of settings.py.


![Screen Shot 2020-10-12 at 4.49.23 PM](https://dev-to-uploads.s3.amazonaws.com/i/4quby5v6rok0gbat0px2.png)
 

I have also added my app to the installed apps section. Events is the name of my app; you might have a different name or the same depending on what you have chosen when creating your app.

If you look at the path at the top of the code snippet, you’ll see different names. Let me explain them quickly here.

I have a project folder which is the main folder that contains all files for the project. Inside this folder, I have the wgo (name of my project) folder and the events (name of my app) folder. Inside the wgo folder, you will find settings.py.

**Event Model**

Open models.py from your app’s folder and create a model.


![Screen Shot 2020-10-12 at 4.54.37 PM](https://dev-to-uploads.s3.amazonaws.com/i/w04687pzklvwpf1uu42t.png)
 

After creating your model, let’s register it to the admin panel, so that we can start creating.

Open admin.py from your app’s folder and add the following code.


![Screen Shot 2020-10-12 at 5.27.24 PM](https://dev-to-uploads.s3.amazonaws.com/i/o2ifi6nk5utujue7uhwc.png)
 

In the terminal window, run the following commands to create the model.


![Screen Shot 2020-10-12 at 5.31.25 PM](https://dev-to-uploads.s3.amazonaws.com/i/bq0q43picdc5oh7jwb96.png)
 

Afterwards, create a superuser by issuing the command below and follow the steps to create the superuser.


![Screen Shot 2020-10-12 at 5.31.37 PM](https://dev-to-uploads.s3.amazonaws.com/i/2i6ji3td9osa71wn7atv.png)
 

Go to http://localhost:8000/admin/ and create a couple of entries. After you have created them you should have a similar page as the one shown below.


![j](https://dev-to-uploads.s3.amazonaws.com/i/mrx5u79yyvc0t012wdg8.png)
 

**Event Serializer**

We create a serializer so that we can convert the data to an appropriate API format such as JSON or XML. 

Create serializers.py in your app’s folder and add the following code.


![Screen Shot 2020-10-12 at 5.33.17 PM](https://dev-to-uploads.s3.amazonaws.com/i/218w1d3xm3r5l4x0m35l.png)


Make sure that you import serializers from rest_framework. Also, import the model instance you want to work with. 

By using the ModelSerializer class, we can easily convert the data of any model. We have chosen to serialize all fields of the model, but you can choose specific fields as well.


**Event Views**

Let’s create the necessary views for the different CRUD operations.

Open views.py from your app’s folder and add the following code.


![Screen Shot 2020-10-12 at 5.36.01 PM](https://dev-to-uploads.s3.amazonaws.com/i/3krnprtb7y8482uv4oa4.png)
 

We import Response from rest_framework in order to render the content as requested and also import the api_view decorator since we are using function-based views. This decorator lets Django know which type of request we are implementing such as  GET, POST, PUT, or DELETE.

We have created different CRUD views by using database queries. In Django REST framework, we use serializers to manage the data being passed.

For an explanation of database queries, you can check this tutorial.

[Django Takeoff Series - Chapter 4 - Database Queries ](https://dev.to/balt1794/chapter-4-listings-page-django-3-2-1-takeoff-series-3c12)


After having created the CRUD views, the last step is to create the necessary URLs for these views.


**Event URLs**


Create urls.py in your app’s folder and add the following code.


![Screen Shot 2020-10-12 at 5.36.48 PM](https://dev-to-uploads.s3.amazonaws.com/i/98wojh544kq6snyfgrwk.png) 


We have imported all views and created a path for each one of them as shown above.

Finally, open the root urls.py from your project’s folder and add the path to the app’s URLs. 


![Screen Shot 2020-10-12 at 5.36.56 PM](https://dev-to-uploads.s3.amazonaws.com/i/2962s3i6tszdr3gahwg6.png)
 

Run server by issuing the following command.


![Screen Shot 2020-10-12 at 5.37.05 PM](https://dev-to-uploads.s3.amazonaws.com/i/qssgvb4ro2ptukxwweq5.png)
 

Let’s check that all views are working properly.


**Events List View**

Go to http://localhost:8000/api/events/ to see all entries.
 

![Screen Shot 2020-10-12 at 5.39.07 PM](https://dev-to-uploads.s3.amazonaws.com/i/qvftf6j37afczviz446c.png)
 

**Detail View**


Go to http://localhost:8000/api/events/detail/1/ to see individual entries. Change the number at the end of the URL for each individual entry. 
 
![efdf](https://dev-to-uploads.s3.amazonaws.com/i/7a49d3cxokh4xnrqpojo.png)
 
 
**Create View**


Go to http://localhost:8000/api/events/create to create new entries. You can copy an entry from the entries list and modify it as shown below.

You can delete the id field or leave it. Either way, when submitting the data, the id will be updated to the correct entry id number. Submit the entry by clicking POST.


![Screen Shot 2020-10-12 at 5.40.25 PM](https://dev-to-uploads.s3.amazonaws.com/i/2phbapguzpokrj6wxivs.png)
 

After you submit the entry, you can see that the id has been updated to the correct number. In my case, the entry id is 6. This is because I have been creating and deleting multiple entries for testing purposes. Your entry id number might be different.


![Screen Shot 2020-10-12 at 5.40.32 PM](https://dev-to-uploads.s3.amazonaws.com/i/b7jzolhm4s3qto6yckbe.png)
 

**Update View**


Go to http://localhost:8000/api/events/update/2/ to update entries. Change the number at the end of the URL for each individual entry. Copy an entry from the entries list and change any field or all fields, and then click POST.


![Screen Shot 2020-10-12 at 5.40.57 PM](https://dev-to-uploads.s3.amazonaws.com/i/zzswuh90306zdqkqns02.png)
 

After submitting the data, the chosen entry should be updated with the changes as shown below.


![Screen Shot 2020-10-12 at 5.41.04 PM](https://dev-to-uploads.s3.amazonaws.com/i/8qvy5kpa7q27ebixakz4.png)
  

**Delete View**


Go to http://localhost:8000/api/events/delete/2/ to delete entries. Change the number at the end of the URL for each individual entry.

I chose entry #2 to be deleted. You can choose any entry. Click on delete to remove the entry.


![Screen Shot 2020-10-12 at 5.41.32 PM](https://dev-to-uploads.s3.amazonaws.com/i/8w0v1z6pvcer7fkdovmq.png)
 

After you have deleted the entry, a ‘Deleted’ message will be shown. You can check that the entry has been removed by going to the entries list page. 


![Screen Shot 2020-10-12 at 5.41.38 PM](https://dev-to-uploads.s3.amazonaws.com/i/i1i0ff9gzi8k9cj478a1.png)
 

Leave any questions in the comments and share if you find this post helpful.


Learn more about Django:

[Django Takeoff Series - Overview ](https://dev.to/balt1794/django-3-2-1-takeoff-series-beginner-friendly-overview-59ip)

[Django 3..2..1..Takeoff Book](https://amzn.to/34UAIe2)

[Personal Website](https://baltlogs.com)

[Twitter](https://twitter.com/balt1794)