Django RESTful Web Services
eBook - ePub

Django RESTful Web Services

Gaston C. Hillar, Norbert Máté

Share book
  1. 326 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Django RESTful Web Services

Gaston C. Hillar, Norbert Máté

Book details
Book preview
Table of contents
Citations

About This Book

Design, build and test RESTful web services with the Django framework and Python

Key Features

  • Create efficient real-world RESTful web services with the latest Django framework
  • Authenticate, secure, and integrate third-party packages efficiently in your Web Services
  • Leverage the power of Python for faster Web Service development

Book Description

Django is a Python web framework that makes the web development process very easy. It reduces the amount of trivial code, which simplifies the creation of web applications and results in faster development. It is very powerful and a great choice for creating RESTful web services.

If you are a Python developer and want to efficiently create RESTful web services with Django for your apps, then this is the right book for you.

The book starts off by showing you how to install and configure the environment, required software, and tools to create RESTful web services with Django and the Django REST framework. We then move on to working with advanced serialization and migrations to interact with SQLite and non-SQL data sources. We will use the features included in the Django REST framework to improve our simple web service.

Further, we will create API views to process diverse HTTP requests on objects, go through relationships and hyperlinked API management, and then discover the necessary steps to include security and permissions related to data models and APIs. We will also apply throttling rules and run tests to check that versioning works as expected. Next we will run automated tests to improve code coverage.

By the end of the book, you will be able to build RESTful web services with Django.

What you will learn

  • The best way to build a RESTful Web Service or API with Django and the Django REST Framework
  • Develop complex RESTful APIs from scratch with Django and the Django REST Framework
  • Work with either SQL or NoSQL data sources
  • Design RESTful Web Services based on application requirements
  • Use third-party packages and extensions to perform common tasks
  • Create automated tests for RESTful web services
  • Debug, test, and profile RESTful web services with Django and the Django REST Framework

Who this book is for

This book is for Python developers who want to create RESTful web services with Django; you need to have a basic working knowledge of Django but no previous experience with RESTful web services is required.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Django RESTful Web Services an online PDF/ePUB?
Yes, you can access Django RESTful Web Services by Gaston C. Hillar, Norbert Máté in PDF and/or ePUB format, as well as other popular books in Informatica & Sviluppo web. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788835572
Edition
1
Subtopic
Sviluppo web

Working with Advanced Relationships and Serialization

In this chapter, we will create a complex RESTful Web Service that will persist data in a PostgreSQL database. We will work with different types of relationships between the resources and we will take advantage of generic classes and generic views provided by the Django REST framework to reduce the amount of boilerplate code. We will gain an understanding of:
  • Defining the requirements for a complex RESTful Web Service
  • Creating a new app with Django
  • Configuring a new web service
  • Defining many-to-one relationships with models.ForeignKey
  • Installing PostgreSQL
  • Running migrations that generate relationships
  • Analyzing the database
  • Configuring serialization and deserialization with relationships
  • Defining hyperlinks with serializers.HyperlinkedModelSerializer
  • Working with class-based views
  • Taking advantage of generic classes and generic views
  • Generalizing and mixing behavior
  • Working with routing and endpoints
  • Making requests that interact with resources that have relationships

Defining the requirements for a complex RESTful Web Service

So far, our RESTful Web Service performed CRUD operations on a single database table. Now, we want to create a more complex RESTful Web Service with the Django REST framework to interact with a complex database model.
A drone is an IoT (short for Internet of Things) device that interacts with many sensors and actuators, including digital electronic speed controllers linked to engines, propellers, and sensors. A drone is also known as an Unnamed Aerial Vehicle (UAV). We will code a RESTful Web Service that will allow us to register competitions for drones that are grouped into drone categories. In our previous RESTful Web Service, we had toys grouped in toy categories and we used a string field to specify the toy category for a toy. In this case, we want to be able to easily retrieve all the drones that belong to a specific drone category. Thus, we will have a relationship between a drone and a drone category.
We must be able to perform CRUD operations on diverse resources and resource collections. Many resources have relationships with other resources, and therefore, we won't work with simple models. We will learn how to establish different kinds of relationships between the models.
The following list enumerates the resources and the model name we will use to represent them in a Django REST framework:
  • Drone categories (DroneCategory model)
  • Drones (Drone model)
  • Pilots (Pilot model)
  • Competitions (Competition model)
The drone category (DroneCategory model) just requires a name.
We need the following data for a drone (Drone model):
  • A foreign key to a drone category (DroneCategory model)
  • A name
  • A manufacturing date
  • A bool value indicating whether the drone participated in at least one competition or not
  • A timestamp with the date and time in which the drone was inserted in the database
We need the following data for a pilot (Pilot model):
  • A name
  • A gender value
  • An integer value with the number of races in which the pilot participated
  • A timestamp with the date and time in which the pilot was inserted in the database
We need the following data for the competition (Competition model):
  • A foreign key to a pilot (Pilot model)
  • A foreign key to a drone (Drone model)
  • A distance value (measured in feet)
  • A date in which the drone controlled by the pilot reached the specified distance value
We will use diverse options that the Django REST framework provides us to materialize the relationship between resources. This way, we will be able to analyze different configurations that will make it possible for us to know which is the best option based on the specific requirements of new web services that we will develop in the future.
The following table shows the HTTP verbs, the scope and the semantics for the methods that our new RESTful Web Service must support. Each method is composed by an HTTP verb and a scope. All the methods have well-defined meanings for all the resources and resource collections. In this case, we will implement the PATCH HTTP verb for all the resources:
HTTP verb
Scope
Semantics
GET
Drone category
Retrieve a single drone category. The drone category must include a list of URLs for each drone resource that belongs to the drone category.
GET
Collection of drone categories
Retrieve all the stored drone categories in the collection, sorted by their name in ascending order. Each drone category must include a list of URLs for each drone resource that belongs to the drone category.
POST
Collection of drone categories
Create a new drone category in the collection.
PUT
Drone category
Update an existing drone category.
PATCH
Drone category
Update one or more fields of an existing drone category.
DELETE
Drone category
Delete an existing drone category.
GET
Drone
Retrieve a single drone. The drone must include its drone category description.
GET
Collection of drones
Retrieve all the stored drones in the collection, sorted by their name in ascending order. Each drone must include its drone category description.
POST
Collection of drones
Create a new drone in the collection.
PUT
Drone
Update an existing drone.
PATCH
Drone
Update one or more fields of an existing drone.
DELETE
Drone
Delete an existing drone.
GET
Pilot
Retrieve a single pilot. The pilot must include a list of the registered competitions, sorted by distance in descending order. The list must include all the details for the competition in which the pilots and his related drone participated.
GET
Collection of pilots
Retrieve all the stored pilots in the collection, sorted by their name in ascending order. Each pilot must include a list of the registered competitions, sorted by distance in descending order. The list must include all the details for the competition in which the pilot and his related drone participated.
POST
Collection of pilots
Create a new pilot in the collection.
PUT
Pilot
Update an existing pilot.
PATCH
Pilot
Update one or more fields of an existing pilot.
DELETE
Pilot
Delete an existing pilot.
GET
Competition
Retrieve a single competition. The competition must include the pilot's name that made the drone reach a specific distance and the drone's name.
GET
Collection of competitions
Retrieve all the stored competitions in the collection, sorted by distance in descending order. Each competition must include the pilot's name that made the drone reach a specific distance and the drone's name.
POST
Collection of competitions
Create a new competition in the collection. The competition must be related to an existing pilot and an existing drone.
PUT
Competition
Update an existing competition.
PATCH
Competition
Update one or more fields of an existing competition.
DELETE
Competition
Delete an existing competition.
In the previous table, we have a huge number of methods and scopes. The following table enumerates the URIs for each scope mentioned in the previous table, where {id} has to be replaced with the numeric id or primary key of the resource:
Scope
URI
Collection of drone categories
/drone-categories/
Drone category
/drone-category/{id}
Collection of drones
/drones/
Drone
/drone/{id}
Collection of pilots
/pilots/
Pilot
/pilot/{id}
Collection of competitions
/competitions/
Competition
/competition/{id}
Let's consider that http://localhost:8000/ is the URL for the RESTful Web Service running on Django's development server. We have to compose and send an HTTP request with the following HTTP verb (GET) and request URL (http://localhost:8000/competitions/) to retrieve all the stored competitions in the collection.
GET http://loca...

Table of contents