Django implements interface automation platform (ten) custom action names [continuously updated]

related articles:

Django implements interface automation platform (nine) environment envs serializer and view [continuously updated]_Do test meow sauce blog-CSDN blog

In-depth understanding of the Mixin class in DRF Develop Paper 

The use of Mixin class in python - Programmer Sought 

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. Background

Student information (id, name, age, address)

When viewing the data list (student list), two display forms are required.

Form 1: directly display the list of student details.list()

Form 2: Only display the list of id and name student information.names()

Form 1 and form 2 have different serializers.

So customize the names() action to correspond to another serializer

Two, custom action

# -*- coding: utf-8 -*-

import os
from datetime import datetime

from django.conf import settings
from rest_framework.decorators import action
from rest_framework.response import Response

from envs.models import Envs
from utils import common


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)

1. Custom names action

2. When the action is names:

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:

 3. Mixin application

Mixin class needs to be used together with other classes, view class:

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

The get_serializer_class method is also a method of the overridden GenericAPIView(views.APIView) class.

A total of three methods have been rewritten

  • Paging function: def paginate_queryset(self, queryset)
  • Filter function: def filter_queryset(self, queryset)
  • Select serializer: def get_serializer_class(self):

Paging and filtering are rewritten under the custom Mixin class for the convenience of encapsulation, which is universal

Select the serializer to rewrite under the view class of the specific application. This is personalized. Different view classes need to specify different serializers.

Guess you like

Origin blog.csdn.net/qq_39208536/article/details/131728607