且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django-rest-swagger不适用于modelserializer吗?

更新时间:2023-09-04 10:35:46

在您的代码中:





此文本是此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 *** 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.