We will quickly go through a simple tutorial to create django rest project. It is extremely easy to configure a rest project work with Djange rest framework. Lets go through the steps. I am using PyCharm but it is not required but it will make development much easier with PyCharm. So lets begin.
Create a new project in PyCharm. Those not using PyCharm can skip this step. Create a directory otherwise.
Install django using command “pip install django==3.1” on the terminal. You should see a successful installation of the django library.
Now create a django project using command on terminal “django-admin startproject restproject .” “.” is used to create project in the current directory
Now that we have the project ready, we will create a django app. The project consist only the configuration information. The app contains the application code. Execute the command “python manage.py startapp products“
Lets now create a model to store and retrieve. Lets create a model with name “Product” with fields “name”, “price”, “desc” as per the below screenshot.
Now, execute the command “python manage.py makemigrations” and “python manage.py migrate” . You will see following output. This will create a table in sqlite3 db being used in django.
Install the django rest framework executing the command “pip install djangorestframework”
Create a new file with name “serializers.py” under the products app as shown in the snapshot below.
The “serializers” are used to transform between various formats between the rest client and the server so that communication can happen seamlessly. Here we create a class ProductSerializer class which extends from the ModelSerializer class. The ModelSerializer class maps the fields between the model and serializer and performs validation based on the model defined under the meta class. We don’t need to think much here, just we need to understand that this is required for transofmation between an object to json/xml or vice versa.
Now update the views.py file under the products app as below. Here we create a new function “product_list” which takes request as input parameter. We retrieve all the elements in the products table and then use the ProductSerializer created in the previous step to create a json response. Please see 1,2,3 points in the snapshot. We annotate the function to process “GET” request with api_view
Now create a urls.py file in the products app if not present already and map “products” url to “product_list” function we created in the views.py file
Now update the urls.py file in the restproject as below to map urls starting with api to the products app
We are done with the rest framework basic setup. To demonstrate its working, lets add some values to the products object/table using the admin functionality provided by django.
First register your “product” object to the admin. For this create a file admin.py file under products app and update with below content.
Lets create a superuser to manage admin functionality provided by django. Execute below commands. Provide details of username and password to create a super user
Now lets run our server with command “python manage.py runserver“. This will run the server at http://localhost/8000
Now navigate to url http://127.0.01:8000/admin. you will see below below screenshot and provide username and password created in the step above. for me it is admin/admin. Provide the details and press login.
Now you should see below screenshot
Now lets add a few products to the list, click on add and fill the details
This should add to the products table. This is really easy and fast way to populate some data. The admin functionality can do a lot of this, but will see more of it in future tutorials.
Now we are all set to retrieve the list of the products using django rest framework. Hit the url http://127.0.0.1:8000/api/products to see the products we have created in previous step. You should response in json for the products you have created.
We have just now successfully created a basic setup of django restframework. We will look at some advanced features in the next tutorial. The git link for this project is “https://github.com/letsblogcontent/SampleRestProject“