How do I add a parameter in the django rest framework documentation?

636 Views Asked by At

I installed coreapi in my django application. I have set the url normally as the default in the documentation: http://www.django-rest-framework.org/topics/documenting-your-api/

When I open the url, a page with the "list - GET" and "create - POST" parameters is returned to me. However, within the GET parameters, I can not see the filters I've added. How do I show the filters in the GET parameter table? Currently only the "page" and "page_size" parameters appear, but I need to also show "matricula", "ano", "vinculo" and "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

There are 0 best solutions below