django-rest-swagger不适用于modelserializer吗? [英] Does django-rest-swagger not work well with modelserializers?

查看:219
本文介绍了django-rest-swagger不适用于modelserializer吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经离开了django-rest-swagger的文档 github page ,更具体地说是如何工作的部分。它显示您可以为您的休息api定义自己的参数,并将这些参数显示在您的swagger doc页面中。



评论示例如下:

 
这个文本是这个API的描述
param1 - 第一个参数
param2 - 第二个参数

/ pre>

我可以让这个工作,但我的问题是如何指定变量是否需要,其参数类型及其数据类型。 github页面显示一个示例图像,你的swagger doc可以看起来,他们有我刚才提到的信息。但是当我给我的自定义参数发表评论时,我的参数只显示为参数类型:query,数据类型为空,不显示必需。



最近找到的答案是这个stackoverflow问题。似乎一个答案提供者正在说django-rest-swagger通过自动检查你的序列化程序来生成它的文档(这是很好的),而且模型序列化程序不会包含足够的django-rest-swagger信息来正确地导出我提到的标准以上。我得到它不能确定这个标准,但是我必须有一些方法来手动指定它。



我正确的说,django-rest-swagger会只有当我将我的modelserializer重写为序列化程序时才显示我想要的?我没有办法手动告诉django-rest-swagger一个参数的参数类型和数据类型应该是什么,如果需要的话?



我知道我一定是在这里遗漏的东西我使用基于类的视图和modelserializer,它们与django-rest-framework教程中的示例几乎相同。在这种情况下,我似乎完全不了解参数类型。我的API工作非常好,我不想将我的modelserializer重写为序列化程序,所以我可以通过swagger获得更好的自动文档。

解决方案

在您的代码中:





此文本是此API的说明

param1 - 第一个参数

param2 - 第二个参数



尝试:



这个文本是这个API的描述

param1 - 第一个参数

param2 - 第二个参数



我发现一些python和/或Django插件需要docstring的第一行,这是打开三个双引号也是启动文档的行。你甚至可能试图在最后一个双引号和T之间没有空格。


I've been going off of the documentation on the django-rest-swagger github page, more specifically the part called "How it works". It shows that you can define your own parameters for your rest api, and have those parameters show up in your swagger doc page.

The commenting example is something like:

"""    
This text is the description for this API    
param1 -- A first parameter    
param2 -- A second parameter    
"""    

I can get this to work, but my issue is how to specify if the variable is required, its parameter type, and its data type. The github page shows an example image of how your swagger doc could look, and they have the information I just mentioned. But when I comment my custom parameters like the example shows, my parameters just show as parameter type: "query", data type: is blank, and it doesn't show "required".

The closest thing I have found to an answer was in this stackoverflow question. It seems like an answer provider is saying that django-rest-swagger generates its documentation by automatically inspecting your serializers (which is fine), and that modelserializers won't contain enough information for django-rest-swagger to properly derive the criteria I mentioned above. I get that it can't figure out this criteria but there must be some way for me to manually specify it then.

Am I correct that django-rest-swagger would only display what I want if I rewrote my modelserializers as just serializers? Is there no way for me to manually tell django-rest-swagger what a parameter's parameter type and data type should be, and if it's required?

I know I must be missing something here. I use class-based views and modelserializers that are almost identical to the examples in the django-rest-framework tutorials. It seems entirely possible that I'm just missing an understanding of "parameter types" in this context. My API is working great and I don't want to rewrite my modelserializers to serializers just so I can get better automatic documentation through swagger.

解决方案

In the code you have:

"""
This text is the description for this API
param1 -- A first parameter
param2 -- A second parameter
"""

Try:

""" This text is the description for this API
param1 -- A first parameter
param2 -- A second parameter
"""

I have found some python and/or Django plugins need the docstring's first line, which is the one with the opening three double-quotes to also be the line that starts the documentation. You might even want to try no space between the last double-quote and the T.

这篇关于django-rest-swagger不适用于modelserializer吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆