如何在django rest框架文档中添加参数?

时间:2018-09-24 23:27:26

标签: django django-rest-framework core-api

我在我的django应用程序中安装了coreapi。我通常在文档中将网址设置为默认网址:http://www.django-rest-framework.org/topics/documenting-your-api/

当我打开URL时,将返回一个带有“列表-GET”和“创建-POST”参数的页面。但是,在GET参数中,看不到添加的过滤器。如何在GET参数表中显示过滤器?当前仅显示“ page”和“ page_size”参数,但我还需要显示“ matricula”,“ ano”,“ vinculo”和“ mes”。

urls.py

from django.conf.urls import url
from django.urls import path
from rest_framework.documentation import include_docs_urls

from api.views import FuncionarioList

urlpatterns = [
    path('api/funcionarios/folha', FuncionarioList.as_view()),
    url(r'^api/docs', include_docs_urls(title='Documentação API')),
]

views.py

from django.db.models import Q

from rest_framework import generics
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticatedOrReadOnly

from api.core.serializer import FuncionarioSerializer
from pessoal.models import Funcionario


class StandardResultsSetPagination(PageNumberPagination):
    page_size = 100
    page_size_query_param = 'page_size'
    max_page_size = 1000


class FuncionarioList(generics.ListCreateAPIView):
    """
    get:
    List Funcionarios - Folha

    """
    serializer_class = FuncionarioSerializer
    permission_classes = (IsAuthenticatedOrReadOnly,)
    pagination_class = StandardResultsSetPagination

    def get_queryset(self):
        # Busca Todos
        queryset = Funcionario.objects.all()
        # Filtros
        matricula = self.request.query_params.get('matricula', None)
        ano = self.request.query_params.get('ano', None)
        mes = self.request.query_params.get('mes', None)
        vinculo = self.request.query_params.get('vinculo', None)
        # Lista de Filtros
        filtro_api = (Q())
        # Validadores
        if matricula:
            filtro_api.add(Q(matricula=matricula), Q.AND)
        if ano:
            filtro_api.add(Q(ano=ano), Q.AND)
        if mes:
            filtro_api.add(Q(mes=mes), Q.AND)
        if vinculo:
            filtro_api.add(Q(vinculo=vinculo), Q.AND)
        # Condição para aplicação do filtro
        if filtro_api is not None:
            queryset = queryset.filter(filtro_api)
        # retornar busca
        return queryset

serializer.py

from rest_framework import serializers

from pessoal.models import Funcionario


class FuncionarioSerializer(serializers.ModelSerializer):

    class Meta:
        model = Funcionario
        fields = ('id', 'matricula', 'nome', 'cargo', 'data_admissao', 'salario_fixo','tota_bruto','total_desconto','liquido','data','vinculo')

0 个答案:

没有答案