Django-rest-swagger doesn't seem to work for me. I can't get him to document anything outside of the name

It seems django-rest-swagger has given up support for the YAML documentation and replaced it with an indefinite non-documentary way of doing something. I spent the last 48 hours trying to figure out how I can document the parameters that go into my publishing methods.

For example: I have this:

class user_addresses(APIView):
    """
    get all addresses or post a new one
    """
    authentication_classes = ([JSONWebTokenAuthentication])

    def get(self, request, format=None):
        addresses = Address.objects.filter(owner_id=request.user.id)
        print (addresses)
        serializer = address_serializer(addresses, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = address_serializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({'success': True,
                            'result': serializer.validated_data},
                            status=status.HTTP_201_CREATED)
        return Response({'success': False,
                        'result': serializer.errors},
                        status=status.HTTP_400_BAD_REQUEST)

But django-rest-swagger will show it as:

All parameters are ignored, and I can not add anything to it except the name in the code comment.

Can someone point me towards something that works, where I can define all the rich data that swagger allows, such as the names of post fields, if they are required or not. etc. I'm just going crazy, running around in circles and can't find anything but complaints that there is no way to do this.

+6
2

, 2.0 , CoreAPI, "" , swagger.

CoreAPI serializer view . , , , , help_text:

some_field = serializers.Field(help_text='Field description')

, APIView . , serializer_class, . :

# serializer
class AddressSerializer(serializers.ModelSerializer):

    line1 = serializers.CharField(help_text='Field documentation!')

    class Meta:
        model = Address
        fields = '__all__'
        read_only_fields = 'owner',

    def create(self, validated_data):
        validated_data['owner'] = self.context['request'].user
        return super().create(validated_data)


# api class-based view
class UserAddresses(generics.ListCreateAPIView):
    """
    General API documentation (not wisible in the swagger view)

    get:
    GET-specific documentation!

    Lorem ipsum

    post:
    POST-specific documentation!

    Dolor **sit amet**
    """
    authentication_classes = ([JSONWebTokenAuthentication])
    permission_classes = permissions.IsAuthenticated,
    serializer_class = AddressSerializer

    def get_queryset(self):
        return Address.objects.filter(owner_id=self.request.user.id)

docstirng, , , . , :

enter image description here

+8

A CoreAPI Swagger. Swagger coreapi json . Django Rest Swagger Python CoreAPI JSON (https://github.com/core-api/python-client).

coreapi.Document?

API coreapi.Link(). Link :

  • URL
  • HTTP-

coreapi.Field(). Field :

  • ( )
  • ( )

Swagger , CoreAPI:

import coreapi

def api_schema_generator():
    api_schema = coreapi.Document(
        title="My Swagger",
        content={
            "User Addresses": {

                "int_api_get": coreapi.Link(
                    url="/int_api/v1/addresses/",
                    action="get",
                    description="Get addresses of a user",
                    fields=[
                        coreapi.Field(
                            name="user_id",
                            required=True,
                            location="path",
                            description="Unique ID of the user whose addresses are to be found"
                        ),
                    ]
                ),
                "int_api_post": coreapi.Link(
                    url="/int_api/v1/addresses/",
                    action="post",
                    description="Add address for a user",
                    fields=[
                        coreapi.Field(
                            name="user_id",
                            required=True,
                            location="path",
                            description="Unique ID of the user"
                        ),
                        coreapi.Field(
                            name="address",
                            required=True,
                            location="path",
                            description="Address of the user"
                        ),
                    ]
                )
            }
        }
    )
    return api_schema

coreapi.Document . SwaggerUIRenderer, OpenAPIRenderer CoreJSONRenderer.

views.py

from rest_framework.decorators import api_view, renderer_classes
from rest_framework_swagger import renderers as swagger_renderer
from rest_framework import renderers

@api_view()
@renderer_classes([renderers.CoreJSONRenderer,
                   swagger_renderer.OpenAPIRenderer,
                   swagger_renderer.SwaggerUIRenderer,
                   ])
def schema_view(request):
    api_schema = api_schema_generator()
    return response.Response(api_schema)

, , - URL .

urls.py

from django.conf.urls import include, url

urlpatterns = [
    url(r'^$', views.schema_view),
]

swagger , , Swagger.

+5

All Articles