Django,  Python

Django Rest Framework

https://www.django-rest-framework.org/community/tutorials-and-resources/

This tutorial was used for 50%, the rest is my own demo:

https://tests4geeks.com/django-rest-framework-tutorial/

Set up django:

  • mkvirtualenv dapi
  • navigate to your folder, then mkdir dapi
  • cd dapi, setprojectdir .
  • pip install django
  • django-admin startproject api_apps

Then you get the folder structure, run it:

  • python manage.py runserver
  • Go to http://localhost:8000/

The install worked successfully! Congratulations!

Then run the command:

  • python manage.py migrate

Then we can start to create an app

  • python manage.py startapp demo

Now we have default project for django

Lets make some models, the Bag and Item class, and also add the apps to settings.py “demo.apps.DemoConfig”, then run:

python manage.py makemigrations demo

If that has no errors then apply the migration.

python manage.py migrate

python manage.py runserver

We now need to register the models within django:

Run

  • python manage.py makemigrations demo
  • python manage.py migrate

Now we need to add a superuser to do CRUD operations.

  • python manage.py createsuperuser
  • python manage.py makemigrations demo
  • python manage.py migrate
  • python manage.py runserver
  • Go to http://localhost:8000/ admin

(I changed the class model to include __str__ method, view picture)

Now we can add a bag and items:


Now we can install REST:

  • pip install djangorestframework
  • add to INSTALLED_APPS the line: “rest_framework”

To perform HTTP-based interaction we have to define rules to convert Django models (python objects) to the json strings and vice versa. This is a serialization/deserialization task. So, let’s define some model-based serializers:

from rest_framework import serializers
from .models import Bag, Item

class BagSerializer(serializers.ModelSerializer):
class Meta:
model = Bag
fields = ‘__all__’

class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ‘__all__’

A viewset is a set of views (controllers in traditional MVC terminology). If you take a look at the ModelViewSet code you’ll see that there’s a lot of functionality automatically added there. You are able to create, view, edit and delete objects in your system (and database). It’s a full CRUD set with http as the interface protocol. Let’s configure two viewsets for our classes:

Now we need to add the view to urls.py, so add the file urls.py to api_apps\demo\urls.py (create this), then change the api_apps\urls.py and add the code fields = ‘__all__’ to both serializer classes if it is not there, it will give an error for version lower then xxx.

  • python manage.py runserver
  • visit http://localhost:8000/admin/
  • visit http://localhost:8000/api/

Now we can view the Nike bag and the caps item we added before from api

Here is the bag from the api, there is also a possibility to post data (the same goes for item):


Now lets post some new items to the bag:

Great, now lets get it in json format, use the get button or url:

  • http://localhost:8000/api/item/?format=json

So to wrap this up.

  • We install django and rest
  • Then we make a model
  • To perform HTTP-based interaction we have to define rules to convert Django models (python objects) to the json strings and vice versa. This is a serialization/deserialization task.
  • The view uses the model and serializer to present the data
  • We define the urls in our app
  • We share the app urls with the project / outer world if you will, so we get to them by HTTP.
  • We also tell the project what apps is available
  • Then we vist the web to get our data

To not get an 404 if you browse http://localhost:8000/ , then add the code to project\urls.py

urlpatterns = [
path(”, admin.site.urls),
path(‘admin/’, admin.site.urls),
path(‘api/’, include(“demo.urls”)),
]

Then you get to the admin page….

Comments Off on Django Rest Framework