Django休息swagger post参数没有显示在Swagger文档上

时间:2017-03-21 18:21:10

标签: django django-rest-framework swagger-2.0 django-swagger

我已经将swagger与django rest框架集成在一起,但是swagger docs并没有创建一个输入框来发布post请求的数据。

我的解析器设置,

REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': (
    'rest_framework.parsers.JSONParser',
    'rest_framework.parsers.FormParser',
    'rest_framework.parsers.MultiPartParser',
),

}

这是我的视图类片段,`

class TeamViewList(APIView, BaseView):
"""
    Class based view to handle all operations related to Team Model
"""
logger = logging.getLogger(__name__)

def get_serializer(self):
    return serializers.TeamSerializer

def post(self, request):
    """
    To create a new  team
    """
    try:

urlMapping中:

urlpatterns = [
url(r'^role/$', rest_views.UserTeamRoleView.as_view(), name='user_team_role'),
url(r'^teams/$', rest_views.TeamViewList.as_view(), name='team_list'),
url(r'^teams/(?P<name>[_a-zA-Z0-9\-]+)$', rest_views.TeamViewDetail.as_view(), name='team_detail'),

我的招摇文件生成, enter image description here

无法将json有效负载作为输入传递给post请求。

2 个答案:

答案 0 :(得分:1)

您必须为视图选择PARSER或将DEFAULT_PARSER_CLASSES添加到您的DRF设置,以便在Swagger上查看表单或json对象。

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework.filters.SearchFilter',
        'rest_framework.filters.OrderingFilter',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'helpers.authentication.TokenAuthentication',
    ),
    'DEFAULT_MODEL_SERIALIZER_CLASS': (
        'rest_framework.serializers.ModelSerializer',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser',
    ),
}

答案 1 :(得分:0)

以下方法应该返回一个不是类的实例:

import java.util.*; public class Problem5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Number(); } } public int Number() { System.out.println("please enter a one digit number"); int placeholder = scanner.nextInt; return placholder; }

顺便说一下,即使使用实例而不是类,我在DRF def get_serializer(self): return serializers.TeamSerializer 上也失败了。但是,它看起来像has been fixed here