related articles:
This chapter is a breakdown of the project. When viewing the content of this chapter, it should be viewed in conjunction with the overall project code:
Python django vue httprunner implements interface automation platform (final version)
1. Application scenarios of the project environment
1.1 Environmental Description:
1. When the project is running, you can choose a set of environments to run
2. In the environment, it is mainly the host of this project
1.2 Interfaces and application scenarios of the environment
interface list
request method | URI | corresponding action | Realize function |
GET | /envs/ | .list() | Query env list |
POST | /envs/ | .create() | Create a piece of data |
GET | /envs/{id}/ | .retrieve() | Retrieve detailed data of an env |
PUT | /envs/{id}/ | update() | Update all fields in a piece of data |
PATCH | /envs/{id}/ | .partial_update() | Update some fields in a piece of data |
DELETE | /envs/{id}/ | .destroy() | delete a piece of data |
GET | /envs/names/ | .names() custom | Query env list |
1. Query env list .list()
GET | /envs/ | .list() | Query env list |
Notice:
1. The list involves pagination
2. Create an env.create()
3. View details and edit .retrieve() update() .partial_update()
- See .retrieve()
- update update() partial_update()
4. env list names
There is already a list, why do you need to customize action names:
- list has pagination, names has no pagination
- The item data displayed by the list, the names only display the id and name
Second, the data model model
models.py
from django.db import models
from utils.base_models import BaseModel
class Envs(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField(verbose_name='环境名称', max_length=200, unique=True, help_text='环境名称')
base_url = models.URLField(verbose_name='请求base url', max_length=200, help_text='请求base url')
desc = models.CharField(verbose_name='简要描述', max_length=200, help_text='简要描述')
class Meta:
db_table = 'tb_envs'
verbose_name = '环境信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
The environment module currently has only one field, which is
base_url = models.URLField(verbose_name='请求base url', max_length=200, help_text='请求base url')
The host address of the entire project
Three, serializer serializers
serializers.py
from rest_framework import serializers
from .models import Envs
class EnvsModelSerializer(serializers.ModelSerializer):
class Meta:
model = Envs
exclude = ('update_datetime',)
extra_kwargs = {
'create_datetime': {
'read_only': True,
'format': '%Y年%m月%d日 %H:%M:%S'
}
}
class EnvsNamesSerializer(serializers.ModelSerializer):
class Meta:
model = Envs
fields = ('id', 'name')
There are two serializers.
- EnvsModelSerializer: display all data normally
- EnvsNamesSerializer: only display id and name, prepared for names action
4. View
4.1 viewsets.ModelViewSet
views.py views
from rest_framework import viewsets
from rest_framework import permissions
from .models import Envs
from . import serializers
from utils.mixins import NamesMixin
class EnvsViewSet(NamesMixin, viewsets.ModelViewSet):
queryset = Envs.objects.all()
serializer_class = serializers.EnvsModelSerializer
permission_classes = [permissions.IsAuthenticated]
def get_serializer_class(self):
if self.action == "names":
return serializers.EnvsNamesSerializer
else:
return self.serializer_class
Implement the following interfaces by inheriting the viewsets.ModelViewSet view class
request method | URI | corresponding action | Realize function |
GET | /envs/ | .list() | Query env list |
POST | /envs/ | .create() | Create a piece of data |
GET | /envs/{id}/ | .retrieve() | Retrieve detailed data of an env |
PUT | /envs/{id}/ | update() | Update all fields in a piece of data |
PATCH | /envs/{id}/ | .partial_update() | Update some fields in a piece of data |
DELETE | /envs/{id}/ | .destroy() | delete a piece of data |
Overridden get_serializer_class method
Method under EnvsViewSet(NamesMixin, viewsets.ModelViewSet)-> ModelViewSet->GenericViewSet->GenericAPIView class.
get_serializer_class source code:
After rewriting, when the action is names, the returned serializer is serializers.EnvsNamesSerializer
4.2 Custom names
Relevant knowledge points:
In-depth understanding of the Mixin class in DRF Develop Paper
The use of Mixin class in python - Programmer Sought
The names interface requires us to customize the action
GET | /envs/names/ | .names() custom | Query env list |
1. Customize NamesMixin:
class NamesMixin:
@action(methods=['GET'], detail=False)
def names(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
return response
def paginate_queryset(self, queryset):
"""
names action禁用分页功能
:param queryset:
:return:
"""
if self.action == "names":
return
else:
return super().paginate_queryset(queryset)
def filter_queryset(self, queryset):
"""
names action禁用过滤功能
:param queryset:
:return:
"""
if self.action == "names":
return self.queryset
else:
return super().filter_queryset(queryset)
2. Custom names action
@action(methods=['GET'], detail=False)
def names(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
return response
3. Paging processing
def filter_queryset(self, queryset):
"""
names action禁用过滤功能
:param queryset:
:return:
"""
if self.action == "names":
return self.queryset
else:
return super().filter_queryset(queryset)
4. Filtration
def filter_queryset(self, queryset):
"""
names action禁用过滤功能
:param queryset:
:return:
"""
if self.action == "names":
return self.queryset
else:
return super().filter_queryset(queryset)
source code:
When the action is names, paging and filtering functions are not processed.
Here def paginate_queryset(self, queryset):
def filter_queryset(self, queryset): method, is rewritten.
rewritten
Method under EnvsViewSet(NamesMixin, viewsets.ModelViewSet)-> ModelViewSet->GenericViewSet->GenericAPIView class.
GenericAPIView source code: