marcgibbons / django-rest-swagger

Swagger Documentation Generator for Django REST Framework: deprecated
https://marcgibbons.com/django-rest-swagger/
BSD 2-Clause "Simplified" License
2.59k stars 601 forks source link

How to add query required/optional parameters? #604

Open fl0wjacky opened 7 years ago

fl0wjacky commented 7 years ago

Hi all, this is my situation:I want to add serval required and optional parameters to my api, but I don't know how to do that.Cause there always only one paremeter 'id'. I want to it like this ->http://ww2.sinaimg.cn/large/4aba2919gw1fbh8y8jk6wj21hi11uk3d.jpg But it like this ->http://ww3.sinaimg.cn/large/4aba2919gw1fbh91ptbeoj21hc0hwn16.jpg Below is my development version: Django==1.10.4 djangorestframework==3.5.3 django-rest-swagger==2.1.0

Safrone commented 7 years ago

The way I managed this is by using filter_backends in the views.

from .filter import MyFilter

class MyViewSet(GenericViewSet):
  filter_backends = (MyFilter,)

    ...

in filter.py

class MyFilter(BaseFilterBackend):
  def get_schema_fields(self, view):
    fields = [
      coreapi.Field(name="access_key", description="Access Key", required=True, location='query'),
      ...
    ]

    return fields

The nice thing is that these filters are composable so you can add more filters to filter_backends = and they will all show up as query parameters.

Not sure if this is the best answer but it's working at least.

Hope that helps!

Safrone commented 7 years ago

Try adding this function to the MyFilter class.

def filter_queryset(self, request, queryset, view): return queryset

Otherwise maybe it could have to do with the renderer_classes? This is how it looks in my program.

@api_view()@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])def swagger_view(request): generator = schemas.SchemaGenerator(title='Analytics API', url="/api", urlconf="analytics_api.urls") return Response(generator.get_schema(request=request))

Let me know if that helps at all.

On Tue, Jan 17, 2017 at 4:17 PM, Hwan Kim notifications@github.com wrote:

Hi @Safrone https://github.com/Safrone,

I got almost same problem. I tried your solution, but it is still not showing up. I followed DRF sample token login code. Here is my code.

views.py

from django.shortcuts import render from rest_framework import parsers, renderers from rest_framework.authtoken.models import Token

from rest_framework.authtoken.serializers import AuthTokenSerializer

from rest_framework.response import Response from rest_framework.views import APIView from rest_framework import permissions from rest_framework.filters import BaseFilterBackend

from .serializers import AuthTokenSerializer

class MyFilter(BaseFilterBackend): def get_schema_fields(self, view): fields = [ coreapi.Field(name="access_key", description="Access Key", required=True, location='query'), ] return fields

class ObtainAuthToken(APIView): filter_backends = (MyFilter,) throttle_classes = () permission_classes = (permissions.AllowAny,) parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,) renderer_classes = (renderers.JSONRenderer) serializer_class = AuthTokenSerializer

def post(self, request, *args, **kwargs):
    serializer = self.serializer_class(data=request.data)
    serializer.is_valid(raise_exception=True)
    user = serializer.validated_data['user']
    token, created = Token.objects.get_or_create(user=user)
    return Response({'token': token.key})

obtain_auth_token = ObtainAuthToken.as_view()

On swagger, there is no parameter. https://www.dropbox.com/s/dcz3adhp0y7zpnq/Screenshot% 202017-01-17%2013.15.34.png?dl=0

Can you please let me know how to fix it? Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/marcgibbons/django-rest-swagger/issues/604#issuecomment-273302492, or mute the thread https://github.com/notifications/unsubscribe-auth/ANZYkVDpXK517lLzanEMOLJWMEq_Akjsks5rTS_8gaJpZM4Lcu8T .

hankya commented 6 years ago

The query parameter doesn't have to be a filter, how can I add it in that case?

rafikmak commented 6 years ago

I have the same problem with my GET query params. I need to specify so of them but no way to do this in a clean manner? The old swagger versions handle this in the docstring, but how to do this now ?

m-haziq commented 6 years ago

In the latest swagger you can do this for optional parameters:

from rest_framework.views import APIView
from rest_framework.schemas import AutoSchema

class CustomView(APIView):
    ...
    schema = AutoSchema(
        manual_fields=[
            coreapi.Field("extra_field", 
            required=True,
            location='query',
            description='City name or airport code.'),
        ]
    )

for function based views, try this customized way: https://github.com/m-haziq/django-rest-swagger-docs/#advance-usage

fanfansj commented 6 years ago

In the latest swagger you can do this for optional parameters:

from rest_framework.views import APIView
from rest_framework.schemas import AutoSchema

class CustomView(APIView):
    ...
    schema = AutoSchema(
        manual_fields=[
            coreapi.Field("extra_field", 
            required=True,
            location='query',
            description='City name or airport code.'),
        ]
    )

for function based views, try this customized way: https://github.com/m-haziq/django-rest-swagger-docs/#advance-usage

you are right, but you should add type for your manual_fields list

fms-santos commented 5 years ago

In the latest swagger you can do this for optional parameters:

from rest_framework.views import APIView
from rest_framework.schemas import AutoSchema

class CustomView(APIView):
    ...
    schema = AutoSchema(
        manual_fields=[
            coreapi.Field("extra_field", 
            required=True,
            location='query',
            description='City name or airport code.'),
        ]
    )

for function based views, try this customized way: https://github.com/m-haziq/django-rest-swagger-docs/#advance-usage

Thanks! This was driving me crazy and I finally got it working with your answer.