- Building RESTful Python Web Services
- Gastón C. Hillar
- 221字
- 2021-08-20 10:24:26
Taking advantage of generic class based views
Go to the gamesapi/games
folder and open the views.py
file. Replace the code in this file with the following code that declares the required imports and the class based views. We will add more classes to this file later. The code file for the sample is included in the restful_python_chapter_02_03
folder:
from games.models import GameCategory from games.models import Game from games.models import Player from games.models import PlayerScore from games.serializers import GameCategorySerializer from games.serializers import GameSerializer from games.serializers import PlayerSerializer from games.serializers import PlayerScoreSerializer from rest_framework import generics from rest_framework.response import Response from rest_framework.reverse import reverse class GameCategoryList(generics.ListCreateAPIView): queryset = GameCategory.objects.all() serializer_class = GameCategorySerializer name = 'gamecategory-list' class GameCategoryDetail(generics.RetrieveUpdateDestroyAPIView): queryset = GameCategory.objects.all() serializer_class = GameCategorySerializer name = 'gamecategory-detail' class GameList(generics.ListCreateAPIView): queryset = Game.objects.all() serializer_class = GameSerializer name = 'game-list' class GameDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Game.objects.all() serializer_class = GameSerializer name = 'game-detail' class PlayerList(generics.ListCreateAPIView): queryset = Player.objects.all() serializer_class = PlayerSerializer name = 'player-list' class PlayerDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Player.objects.all() serializer_class = PlayerSerializer name = 'player-detail' class PlayerScoreList(generics.ListCreateAPIView): queryset = PlayerScore.objects.all() serializer_class = PlayerScoreSerializer name = 'playerscore-list' class PlayerScoreDetail(generics.RetrieveUpdateDestroyAPIView): queryset = PlayerScore.objects.all() serializer_class = PlayerScoreSerializer name = 'playerscore-detail'
The following table summarizes the methods that each class-based view is going to process:

In addition, we will be able to execute the OPTIONS
HTTP verb on any of the scopes.
推薦閱讀
- Java Web開發(fā)學(xué)習(xí)手冊
- Deploying Node.js
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- SoapUI Cookbook
- Android Studio Essentials
- 數(shù)據(jù)結(jié)構(gòu)簡明教程(第2版)微課版
- Windows Forensics Cookbook
- C#實(shí)踐教程(第2版)
- Android移動(dòng)開發(fā)案例教程:基于Android Studio開發(fā)環(huán)境
- Frank Kane's Taming Big Data with Apache Spark and Python
- Getting Started with Python and Raspberry Pi
- 超簡單:用Python讓Excel飛起來(實(shí)戰(zhàn)150例)
- Android技術(shù)內(nèi)幕(系統(tǒng)卷)
- Python數(shù)據(jù)可視化之matplotlib實(shí)踐