Django RESTful Web Services
eBook - ePub

Django RESTful Web Services

Gaston C. Hillar, Norbert Måté

Partager le livre
  1. 326 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Django RESTful Web Services

Gaston C. Hillar, Norbert Måté

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Django RESTful Web Services est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Django RESTful Web Services par Gaston C. Hillar, Norbert MĂĄtĂ© en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatica et Sviluppo web. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788835572
Édition
1
Sous-sujet
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 des matiĂšres