我看到了Flask和Django的swagger文档。在Flask中,我可以设计并记录我手动编写的API(包括参数部分所需的字段,可选字段等)。Swagger API文档
下面是我们在瓶
class Todo(Resource):
"Describing elephants"
@swagger.operation(
notes='some really good notes',
responseClass=ModelClass.__name__,
nickname='upload',
parameters=[
{
"name": "body",
"description": "blueprint object that needs to be added. YAML.",
"required": True,
"allowMultiple": False,
"dataType": ModelClass2.__name__,
"paramType": "body"
}
],
responseMessages=[
{
"code": 201,
"message": "Created. The URL of the created blueprint should be in the Location header"
},
{
"code": 405,
"message": "Invalid input"
}
]
)
我可以选择哪些参数,包括怎么做,哪些不能。 但是我如何在Django中实现相同的功能?Django-Swagger Document in 不好。我的主要问题是如何在Django中编写我的raw-json。
在Django中它自动执行它,它不允许我定制我的json。 如何在Django上实现同样的功能?
这里是models.py文件
class Controller(models.Model):
id = models.IntegerField(primary_key = True)
name = models.CharField(max_length = 255, unique = True)
ip = models.CharField(max_length = 255, unique = True)
installation_id = models.ForeignKey('Installation')
serializers.py
class ActionSerializer(serializers.ModelSerializer):
class Meta:
model = Controller
fields = ('installation',)
urls.py
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from modules.actions import views as views
urlpatterns = patterns('',
url(r'(?P<installation>[0-9]+)', views.ApiActions.as_view()),
)
个views.py
class ApiActions(APIView):
"""
Returns controllers List
"""
model = Controller
serializer_class = ActionSerializer
def get(self, request, installation,format=None):
controllers = Controller.objects.get(installation_id = installation)
serializer = ActionSerializer(controllers)
return Response(serializer.data)
我的问题是
1)如果我需要添加一个字段说xyz
,这是不是在我的模型我怎么加呢?
2)安静类似于第一,如果我需要添加它接受值的B/W 3设置值的字段,即一个下拉菜单。我如何添加它?
3)如何添加一个可选字段? (因为在请求PUT
的情况下,我可能只更新1个字段,其余为空,表示optional
字段)。
4)另外我该如何添加一个接受json字符串的字段,如this api所做的那样?
感谢
我可以用我的硬编码的API做所有这些事情瓶。但是在Django中,它会从我的模型中自动执行,而不是(我相信)可以让我访问自定义我的api。在Flask中,我只需要用手编写API,然后与Swagger集成即可。 在Django中是否存在同样的事情?
像我只需要添加以下json在我的烧瓶代码,它会回答我所有的问题。
# Swagger json:
"models": {
"TodoItemWithArgs": {
"description": "A description...",
"id": "TodoItem",
"properties": {
"arg1": { # I can add any number of arguments I want as per my requirements.
"type": "string"
},
"arg2": {
"type": "string"
},
"arg3": {
"default": "123",
"type": "string"
}
},
"required": [
"arg1",
"arg2" # arg3 is not mentioned and hence 'opional'
]
},
[此](http://stackoverflow.com/questions/34591291/show-maximum-minimum-和-default-in-swagger-for-django-rest-framework/37922613#37922613)答案可能对您有用 – Marosinho