使用Django的Swagger响应消息

时间:2016-09-21 05:19:41

标签: python django python-2.7 swagger swagger-ui

views.py:

from django.http import JsonResponse
from rest_framework import schemas
from rest_framework import status, response
from rest_framework.decorators import api_view, renderer_classes
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer

from DjangoProject.settings import db
from DjangoProjectApp.serializers import RestaurantsSerializer


@api_view()
@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])
def schema_view(request):
    generator = schemas.SchemaGenerator(title='Restaurant API')
    return response.Response(generator.get_schema(request=request))


@api_view(['GET'])
def restaurant_list(request):
    if request.method == 'GET':
        restaurantCollection = db.Restaurant
        restaurantlist = []
        for s in restaurantCollection.find():
            restaurantlist.append(s)
        serializer = RestaurantsSerializer(restaurantlist, many=True)
        return JsonResponse(serializer.data, status=status.HTTP_201_CREATED, safe=False)

models.py:

from django.db import models


class Restaurant(models.Model):
    _id = models.TextField()
    name = models.TextField()
    rating = models.IntegerField()

serializers.py:

from rest_framework import serializers

from DjangoProjectApp.models import Restaurant


class RestaurantsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('_id', 'name', 'rating')

现在,我如何使用Django在Swagger中显示响应消息。

在响应消息中,只有200显示为响应代码。

我需要显示不同状态代码的响应。我需要完整的描述。

提前致谢。

0 个答案:

没有答案