Skip to content
Menu
e-lo [IT Engineer life]
  • Home
    • Note
  • Database
    • T-SQL
    • SQL Server quick
    • SQL server docs
    • MySql quick sheet
    • Postgre
    • InfluxDB
  • Programming
    • MS Azure Powershell
    • MS Azure Command-Line Interface (CLI) doc
    • Python Docs
    • Python Logging
    • Python-cheat-sheet
    • Git-guide
  • Azure
    • MS Windows virtual machines in Azure
    • MS ARM Docs
    • MS ARM Template Docs
    • MS ARM Functions
    • MS Bicep+ARM
    • MS ARM Tutorial
    • MS Deployment scripts (intern/extern)
    • MS Virtual Network
  • Az-nutshell
    • ms-technology-choices-compute-decision-tree
    • ms-data-store-decision-tree
    • ms-data-explorer
    • ms-storage-explorer
    • ms-azure-sql
    • ms-common-data-services
    • ms-azure-mysql-daas
    • ms-sla
    • az paas
    • az glossary-quicksheet
    • az-test-vm-script-quickguide
  • Linux
    • Top CMD’s
    • Useful CMD Linux
    • ss64 Linux
    • Ubuntu
    • 30 things Ubuntu 18.04
    • Bootable Ubuntu USB
    • LinuxFilesystemTreeOverview
  • Sys Admin
    • System Administrator
    • Sys News
  • Zen
    • Not thinking about anything is Zen
e-lo [IT Engineer life]

Django Rest Framework

Posted on June 16, 2019June 17, 2019 by espenk

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….

RSS Azure

  • Scale your cloud-native apps and accelerate app modernization with Azure, the best cloud for your apps May 24, 2022

RSS RabbitMQ

  • RabbitMQ 3.8.15 release

RSS Python

  • PEP 691: JSON-based Simple API for Python Package Indexes May 4, 2022

Tags

5 min (26) Ansible (1) ARM (10) azure (40) cmd (3) Django (4) Docker (1) e-lo (2) Flask (2) Github (9) Grafana (2) Information (7) Information Retrieval (13) JAVA (1) kivy (2) Kotlin (6) linux (15) mobile (2) Natural Language Prossesing (NLP) (2) Net.Core (1) Networking and Security (6) OPC (2) PEP8 (1) Philosophy (3) Python (47) Python Networking and Security (5) Reason (2) RMQ (3) Solr (11) Sql (10) Uncategorized (2) VSC (1) Warframe (2) WMVARE (4) Zabbix (7)

Recent Posts

  • 5 min Logic App Storage Table
  • 5 min Logic App PSQL
  • 5 min Logic App
  • 5 MIN Azure Data Explorer
  • TODO Build a Hash Table in Python With TDD Real Python

Archives

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Photo by Markus Spiske from Pexels "Matrix"

©2022 e-lo [IT Engineer life] | Powered by WordPress & Superb Themes