Building APIs With Django And Django Rest Framework

2y ago
71 Views
9 Downloads
963.90 KB
53 Pages
Last View : 14d ago
Last Download : 3m ago
Upload by : Nixon Dill
Transcription

Building APIs with Django and DjangoRest FrameworkRelease 2.0AgiliqJul 21, 2020

Contents1Introductions1.1 Who is this book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1.2 How to read this book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3332Setup, Models and Admin2.1 Creating a project .2.2 Database setup . . .2.3 Creating models . .2.4 Activating models .34567.55567A simple API with pure Django3.1 The endpoints and the URLS3.2 Connecting urls to the views .3.3 Writing the views . . . . . . .3.4 Using the API . . . . . . . .3.5 Why do we need DRF? . . . .999101011Serializing and Deserializing Data4.1 Serialization and Deserialization .4.2 Creating Serializers . . . . . . .4.3 The PollSerializer in detail4.4 Using the PollSerializer .1313131414Views and Generic Views5.1 Creating Views with APIView . . . . . .5.2 Using DRF generic views to simplify code5.3 More generic views . . . . . . . . . . . .5.4 Next Steps . . . . . . . . . . . . . . . . .1717192123More views and viewsets6.1 A better URL structure . . . . . .6.2 Changing the views . . . . . . .6.3 Introducing Viewsets and Routers6.4 Choosing the base class to use . .6.5 Next steps . . . . . . . . . . . .252525282829Access Control.31i

7.17.27.37.47.589iiCreating a user . . . . . . .Authentication scheme setupThe login API . . . . . . .Fine grained access controlNext steps: . . . . . . . . .3133343536Testing and Continuous Integeration8.1 Creating Test Requests . . . . . . . .8.2 Testing APIs with authentication . . .8.3 Using APIClient . . . . . . . . .8.4 .post and create . . . . . . . . . .8.5 Continuous integration with CircleCI8.6 Setting up CircleCI . . . . . . . . . .8.7 Writing circle configuration file . . .3737383941414142Appendix9.1 Testing and Using API with Postman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9.2 Documenting APIs (with Swagger and more) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .454547

Building APIs with Django and Django Rest Framework, Release 2.0Building APIs with Django and DRF takes over where the Django tutorials stop. In the Django tutorials, you built aregular Django polls app. We will rebuild an API for a similar app.In the chapters to come, we will build a REST(ish) api with authorization, rate limiting, first with pure Django andthen with DRF. We will cover testing, continuous integration, documentation tools and API collaboration tools.Chapters:Contents1

Building APIs with Django and Django Rest Framework, Release 2.02Contents

CHAPTER1IntroductionsBuilding APIs with Django and Django Rest Framework starts where the Django “Polls” tutorial stops, and takes youthrough building the polls app, but this time using APIs. You will learn the basics of Django Rest Framework includingserialization, views, generic views, viewsets, testing, access control. You will also learn about API documentationusing swagger and raml.1.1 Who is this book for?If you have finished the Django “Polls” tutorial, and want to learn using DRF to build APIs, this book is perfect foryou. This book assumes some knowledge of Django and Python, which you should have built if you have finished the“Poll” turtorial. No existing knowledge of DRF is assumed.1.2 How to read this book?The chapters are meant to be read in order. If you have existing knowledge of some chapters, you can quickly gothrough that chapter, but I highly recommend reading them in order as each chapter builds on the previous.3

Building APIs with Django and Django Rest Framework, Release 2.04Chapter 1. Introductions

CHAPTER2Setup, Models and AdminIn this tutorial we will walk through a process of creating an API for a basic poll application. We will be using Python3.6.x, Django 2.0.x and Django Rest Framework 3.7.x for creating API.First things first, let’s install the required modules within a virtual environment.mkvirtualenv pollsapipip install Djangopip install djangorestframework2.1 Creating a projectEarliest in order, to create a project we should move to the directory where we would like to store our code. For thisgo to command line and use cd command. Then trigger the startproject command.django-admin startproject pollsapiThis command gives us a ‘pollsapi’ directoy. The contents of this directory look like this:manage.pypollsapi/init .pysettings.pyurls.pywsgi.py2.2 Database setupWe will use SQlite database, which is already included with Python. The pollsapi/settings.py file wouldalready have the correct settings.5

Building APIs with Django and Django Rest Framework, Release 2.0DATABASES {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE DIR, 'db.sqlite3'),}}Now, use the migrate command which builds the needed database tables in regard to the django pollsapi/settings.py file.python manage.py migrate2.3 Creating modelsBefore creating our database models, let us create our pollsapi App.python manage.py startapp pollsThe above command results in a ‘polls’ directory containing different files:admin.pyapps.pymodels.pytests.pyviews.pyStep in to ‘models.py’ file and start writing the models. For creating the polls api we are going to create a Poll model,a Choice model and a Vote model. Once we are done with designing our models, the models.py file should looklike this:These models are the same as you would have seen in the Django introduction tutorial.from django.db import modelsfrom django.contrib.auth.models import Userclass Poll(models.Model):question models.CharField(max length 100)created by models.ForeignKey(User, on delete models.CASCADE)pub date models.DateTimeField(auto now True)def str (self):return self.questionclass Choice(models.Model):poll models.ForeignKey(Poll, related name 'choices', on delete models.CASCADE)choice text models.CharField(max length 100)def str (self):return self.choice textclass Vote(models.Model):choice models.ForeignKey(Choice, related name 'votes', on delete models.CASCADE)(continues on next page)6Chapter 2. Setup, Models and Admin

Building APIs with Django and Django Rest Framework, Release 2.0(continued from previous page)poll models.ForeignKey(Poll, on delete models.CASCADE)voted by models.ForeignKey(User, on delete models.CASCADE)class Meta:unique together ("poll", "voted by")The above models have been designed in such a way that, it would make our API bulding a smooth process.2.4 Activating modelsWith the simple lines of code in the ‘models.py’ Django can create a database schema and a Python database-accessAPI which has the capability to access the objects of Poll, Choice, Vote. To create the database tables to our models,‘rest framework’ and ‘polls’ app needs to be added to the “INSTALLED APPS” in the ‘django pollsapi/settings’ file.INSTALLED APPS (.'rest framework','polls',)Now, run the makemigrations command which will notify Django that new models have been created and thosechanges needs to be applied to the migration. Run migrate command to do the actual migration. python manage.py makemigrations polls python manage.py migrateCreate an empty urls.py in your polls app.urlpatterns []Go to pollsapi/urls.py and include the polls urls.from django.urls import include, re pathurlpatterns [re path(r' ', include('polls.urls')),]Now you can runserver python manage.py runserverGoto any browser of your choice and hit the url http://127.0.0.1:8000And we are in business, with a Django Congratulations page greeting us. (Though we haven’t added any API endpointsyet.)2.4. Activating models7

Building APIs with Django and Django Rest Framework, Release 2.0We will be adding API endpoints for creating and viewing polls in the next chapter.2.4.1 Setting up the adminYou should register Poll and Choice in the admin like this.from django.contrib import adminfrom .models import Poll, (Choice)8Chapter 2. Setup, Models and Admin

CHAPTER3A simple API with pure DjangoIn this chapter, we will build an API with pure Django. We will not use Django Rest Framework (Or any other library).To start add some Poll using the admin.3.1 The endpoints and the URLSOur API will have two endpoints returning data in JSON format. /polls/ GETs list of Poll /polls/ id / GETs data of a specific Poll3.2 Connecting urls to the viewsWrite two place holder view functions and connect them in your urls.py. We will finish polls list andpolls detail shortly.# In views.pydef polls list(request):passdef polls detail(request, pk):pass# in urls.pyfrom django.urls import pathfrom .views import polls list, polls detailurlpatterns [path("polls/", polls list, name "polls list"),(continues on next page)9

Building APIs with Django and Django Rest Framework, Release 2.0(continued from previous page)path("polls/ int:pk /", polls detail, name "polls detail")]3.3 Writing the viewsWe will now write the polls list and polls detailfrom django.shortcuts import render, get object or 404from django.http import JsonResponsefrom .models import Polldef polls list(request):MAX OBJECTS 20polls Poll.objects.all()[:MAX OBJECTS]data {"results": list(polls.values("question", "created by username", "pub date "))}return JsonResponse(data)def polls detail(request, pk):poll get object or 404(Poll, pk pk)data {"results": {"question": poll.question,"created by": poll.created by.username,"pub date": poll.pub date}}return JsonResponse(data)This should be standard Django for you.polls Poll.objects.all()[:20] gets us upto 20Poll objects. We get a list of dictionaries using {"results": list(polls.values("question","created by username", "pub date"))} and return it with a JsonResponse. A JsonResponse isa like HttpResponse with content-type application/json.Similarly, polls detail gets a specific Poll using get object or 404(Poll, pk pk), and returns it wrappedin JsonResponse.3.4 Using the APIYou can now access the API using curl, wget, postman, browser or any other API consuming tools. Here us theresponse with curl. curl http://localhost:8000/polls/{"results": [{"pk": 1, "question": "What is the weight of an unladen swallow?", "created by username": "shabda", "pub date": "2018-03-12T10:14:19.002Z"}, {"pk": 2, "question": "What do you prefer, Flask or Django?", "created by username": "shabda ", "pub date": "2018-03-12T10:15:55.949Z"}, {"pk": 3, "question": "What is your favorite vacation spot?", "created by username": "shabda", "pub date": "2018-03 12T10:16:11.998Z"}]}You should consider using postman or a similar tool. This is how your API looks in Postman.10Chapter 3. A simple API with pure Django

Building APIs with Django and Django Rest Framework, Release 2.03.5 Why do we need DRF?(DRF Django Rest Framework)We were able to build the API with just Django, without using DRF, so why do we need DRF? Almost always, youwill need common tasks with your APIs, such as access control, serialization, rate limiting and more.DRF provides a well thought out set of base components and convenient hook points for building APIs. We will beusing DRF in the rest of the chapters.3.5. Why do we need DRF?11

Building APIs with Django and Django Rest Framework, Release 2.012Chapter 3. A simple API with pure Django

CHAPTER4Serializing and Deserializing DataDRF makes the process of building web API’s simple and flexible. With batteries included, it comes with well designedbase classes which allows us to serialize and deserialize data.4.1 Serialization and DeserializationThe first thing we need for our API is to provide a way to serialize model instances into representations. Serialization isthe process of making a streamable representation of the data which we can transfer over the network. Deserializationis its reverse process.4.2 Creating SerializersLets get started with creating serializer classes which will serialize and deserialize the model instances to json representations. Create a file named polls/serializers.py. We will use ModelSerializer which will reducecode duplication by automatically determing the set of fields and by creating implementations of the create() andupdate() methods.Our polls/serializers.py looks like this.from rest framework import serializersfrom .models import Poll, Choice, Voteclass VoteSerializer(serializers.ModelSerializer):class Meta:model Votefields ' all 'class tinues on next page)13

Building APIs with Django and Django Rest Framework, Release 2.0(continued from previous page)votes VoteSerializer(many True, required False)class Meta:model Choicefields ' all 'class s ChoiceSerializer(many True, read only True, required False)class Meta:model Pollfields ' all '4.3 The PollSerializer in detailOur PollSerializer looks like this.class s ChoiceSerializer(many True, read only True, required False)class Meta:model Pollfields ' all 'What have we got with this? The PollSerializer class has a number of methods, A is valid(self, .) method which can tell if the data is sufficient and valid to create/update a modelinstance. A save(self, .) method, which knows how to create or update an instance. A create(self, validated data, .) method which knows how to create an instance. This methodcan be overriden to customize the create behaviour. A update(self, instance, validated data, .) method which knows how to update an instance. This method can be overriden to customize the update behaviour.4.4 Using the PollSerializerLet’s use the serializer to create a Poll object.In [1]: from polls.serializers import PollSerializerIn [2]: from polls.models import PollIn [3]: poll serializer PollSerializer(data {"question": "Mojito or Caipirinha?", "created by": 1})In [4]: poll serializer.is valid()Out[4]: True(continues on next page)14Chapter 4. Serializing and Deserializing Data

Building APIs with Django and Django Rest Framework, Release 2.0(continued from previous page)In [5]: poll poll serializer.save()In [6]: poll.pkOut[6]: 5The poll.pk line tells us that the object has been commited to the DB. You can also use the serializer to update aPoll object.In [9]: poll serializer PollSerializer(instance poll, data {"question": "Mojito, Caipirinha or margarita?", "created by": 1})In [10]: poll serializer.is valid()Out[10]: TrueIn [11]: poll serializer.save()Out[11]: Poll: Mojito, Caipirinha or margarita? In [12]: Poll.objects.get(pk 5).questionOut[12]: 'Mojito, Caipirinha or margarita?'We can see that calling save on a Serializer with instance causes that instance to be updated. Poll.objects.get(pk 5).question verifies that the Poll was updated.In the next chapter, we will use the serializers to write views.4.4. Using the PollSerializer15

Building APIs with Django and Django Rest Framework, Release 2.016Chapter 4. Serializing and Deserializing Data

CHAPTER5Views and Generic ViewsIn this chapter, we will create views using APIVIew, and generics.ListCreateAPIView and family.5.1 Creating Views with APIViewTo start with, we will use the APIView to build the polls list and poll detail API we built in the chapter, A simple APIwith pure Django.Add this to a new file polls/apiviews.pyfrom rest framework.views import APIViewfrom rest framework.response import Responsefrom django.shortcuts import get object or 404from .models import Poll, Choicefrom .serializers import PollSerializerclass PollList(APIView):def get(self, request):polls Poll.objects.all()[:20]data PollSerializer(polls, many True).datareturn Response(data)class PollDetail(APIView):def get(self, request, pk):poll get object or 404(Poll, pk pk)data PollSerializer(poll).datareturn Response(data)And change your urls.py to17

Building APIs with Django and Django Rest Framework, Release 2.0from django.urls import pathfrom .apiviews import PollList, PollDetailurlpatterns [path("polls/", PollList.as view(), name "polls list"),path("polls/ int:pk /", PollDetail.as view(), name "polls detail")]DRF comes with a browsable api, so you can directly open http://localhost:8000/polls/ in the browser.It looks like thisYou can now do an options request to /polls/, which gives{"name": "Poll List","description": "","renders": ["application/json","text/html"],"parses": oded","multipart/form-data"]}18Chapter 5. Views and Generic Views

Building APIs with Django and Django Rest Framework, Release 2.0This is how it looks like in postman.5.2 Using DRF generic views to simplify codeThe PollList and PollDetail get the work done, but there are bunch of common operations, we can do it inabstract away.The generic views of Django Rest Framework help us in code reusablity. They infer the response format and allowedmethods from the serializer class and base class.Change your apiviews.py to the below code, and leave urls.py as is.from rest framework import genericsfrom .models import Poll, Choicefrom .serializers import PollSerializer, ChoiceSerializer,\VoteSerializerclass PollList(generics.ListCreateAPIView):queryset Poll.objects.all()serializer class PollSerializerclass et Poll.objects.all()serializer class PollSerializerWith this change, GET requests to /polls/ and /polls/ pk /, continue to work as was, but we have a moredata available with OPTIONS.Do an OPTIONs request to /polls/, and you will get a response like this.5.2. Using DRF generic views to simplify code19

Building APIs with Django and Django Rest Framework, Release 2.0{"name": "Poll List","description": "","renders": ["application/json","text/html"],"parses": oded","multipart/form-data"],"actions": {"POST": {"id": {"type": "integer","required": false,"read only": true,"label": "ID"},// .},"question": {"type": "string","required": true,"read only": false,"label": "Question","max length": 100},"pub date": {"type": "datetime","required": false,"read only": true,"label": "Pub date"},"created by": {"type": "field","required": true,"read only": false,"label": "Created by"}}}}This tells us Our API now accepts POST The required data fields The type of each data field.Pretty nifty! This is what it looks like in Postman.20Chapter 5. Views and Generic Views

Building APIs with Django and Django Rest Framework, Release 2.05.3 More generic viewsLet us add the view to create choices and for voting. We will look more closely at this code shortly.from rest framework import genericsfrom .models import Poll, Choicefrom .serializers import PollSeria

Building APIs with Django and Django Rest Framework starts where theDjango “Polls” tutorialstops, and takes you through building the polls app, but this time using APIs. You will learn the basics of Django Rest Framework including serialization, views, generic views, viewsets, testing, ac

Related Documents:

Session 18 (Working with Django Web Framework) Installing Django, Setting Up Django Working with Web Application Development Django API, Django Admin App, Django Views Django Templates CRUD Operation using Django Working with Django with BootStrap 4.0 Working with Django Forms Django Cl

Python Django Certification Training 3 P a g e About the Program Intellipaat’s Django course makes you master the Django REST framework, Django models, Django AJAX, Django jQuery, etc. In this best Python Django course, you will learn about the web framework by doi

Django ORM Cookbook Documentation, Release 2.0 Django ORM Cookbook is a book about doing things with Django ORM and Django models. Django is a "MTV" (Model-Template-View) framework - This book provides a deep dive into the Mpart. They take the form of about 50 questions of the form How to do X with Django ORM/Queryset/Models. Contents 1

Django Included Apps django.contrib.auth An authentication system. django.contrib.contenttypes A framework for content types. django.contrib.sessions A session framework. django.contrib.sites A framework for managing multiple sites with one Django installation. django

Django Admin Cookbook is a book about doing things with Django admin. It is targeted towards intermediate Django developers, who have some experience with Django admin, but are looking to expand their knowledge of Django

Django Blog – Ch 1-3 Opening Report 02 – 09/07 – 09/13 Django Blog – Ch 1 to 3 Django Deployment – Ch14 Your Django Blog 03 – 09/14 – 09/20 SEO Strategy – Ch 4-6 Django Social Website - Ch 4-6 04 – 09/21 – 09/27 Django Social Website - Ch 4-6 Your Social Site

Capítulo 14: Django desde la línea de comandos. 60 Observaciones 60 Examples 60 Django desde la línea de comandos. 60 Capítulo 15: Django Rest Framework 61 Examples 61 Barebones simples API de solo lectura 61 Capítulo 16: Django y redes sociales 63 Parámetros 63 Examples 64 Manera fácil: python-social-auth 64 Usando Django Allauth 67

Annual Men’s Day Sunday, October 26, 2008 Order of Service Devotion Men’s Day Praise Team Responsive Reading Psalm 1 Congregational Singing This Little Light of Mine Selection Men’s Day Choir Altar Prayer Dance Selection Creations of God Announcements Bro. Robert Shields Pastoral Observations Message Rev. Jeffery A. Lang, Senior Pastor Southside Church & International Ministries Jackson .