- Building RESTful Python Web Services
- Gastón C. Hillar
- 371字
- 2021-08-20 10:24:25
Designing a RESTful API to interact with a complex PostgreSQL database
So far, our RESTful API has performed CRUD operations on a single database table. Now, we want to create a more complex RESTful API with Django REST Framework to interact with a complex database model that has to allow us to register player scores for played games that are grouped into game categories. In our previous RESTful API, we used a string field to specify the game category for a game. In this case, we want to be able to easily retrieve all the games that belong to a specific game category, and therefore, we will have a relationship between a game and a game category.
We should be able to perform CRUD operations on different related resources and resource collections. The following list enumerates the resources and the model names that we will use to represent them in Django REST Framework:
- Game categories (
GameCategory
model) - Games (
Game
model) - Players (
Player
model) - Player scores (
PlayerScore
model)
The game category (GameCategory
) just requires a name, and we need the following data for a game (Game
):
- A foreign key to a game category (
GameCategory
) - A name
- A release date
- A bool value indicating whether the game was played at least once by a player or not
- A timestamp with the date and time in which the game was inserted in the database
We need the following data for a player (Player
):
- A gender value
- A name
- A timestamp with the date and time in which the player was inserted in the database
We need the following data for the score achieved by a player (PlayerScore
):
- A foreign key to a player (
Player
) - A foreign key to a game (
Game
) - A score value
- A date in which the score value was achieved by the player
Tip
We will take advantage of all the resources and their relationships to analyze different options that Django REST Framework provides us when working with related resources. Instead of building an API that uses the same configuration to display related resources, we will use diverse configurations that will allow us to select the most appropriate options based on the particular requirements of the APIs that we are developing.
- Visual Basic .NET程序設(shè)計(jì)(第3版)
- Python概率統(tǒng)計(jì)
- 兩周自制腳本語言
- Python進(jìn)階編程:編寫更高效、優(yōu)雅的Python代碼
- Python Network Programming Cookbook(Second Edition)
- SAS數(shù)據(jù)統(tǒng)計(jì)分析與編程實(shí)踐
- 小程序開發(fā)原理與實(shí)戰(zhàn)
- Java EE核心技術(shù)與應(yīng)用
- Learning PHP 7
- 寫給程序員的Python教程
- Laravel Application Development Blueprints
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇
- Learning Image Processing with OpenCV
- Implementing Microsoft Dynamics NAV(Third Edition)
- Python 3快速入門與實(shí)戰(zhàn)