drf-yasg 自定义 json 正文
drf-yasg custom json body
如何在基于类的 Django 视图中自定义 drf-yasg 模式?
我尝试了这部分代码,但生成的 swagger 不遵守更改。
class CustomView(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'phone': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
'body': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
}))
def create(self, request: Request, *args, **kwargs):
phone = request.data.get('phone')
body = request.data.get('body')
...
我找到了,这是正确的代码:
class CustomView(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
http_method_names = ['post']
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'phone': openapi.Schema(type=openapi.TYPE_STRING, description='The desc'),
'body': openapi.Schema(type=openapi.TYPE_STRING, description='The desc'),
}))
def post(self, request: Request, *args, **kwargs):
phone = request.data.get('phone')
body = request.data.get('body')
...
对于 IN_QUERY 类型,像这样:
class CustomListView(ListAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
http_method_names = ['get']
@swagger_auto_schema(manual_parameters=[
openapi.Parameter('obj_id', openapi.IN_QUERY,
type=openapi.TYPE_STRING),
])
def get(self, request, *args, **kwargs):
obj_id = self.request.DATA.get('obj_id')
如何在基于类的 Django 视图中自定义 drf-yasg 模式?
我尝试了这部分代码,但生成的 swagger 不遵守更改。
class CustomView(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'phone': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
'body': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
}))
def create(self, request: Request, *args, **kwargs):
phone = request.data.get('phone')
body = request.data.get('body')
...
我找到了,这是正确的代码:
class CustomView(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
http_method_names = ['post']
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'phone': openapi.Schema(type=openapi.TYPE_STRING, description='The desc'),
'body': openapi.Schema(type=openapi.TYPE_STRING, description='The desc'),
}))
def post(self, request: Request, *args, **kwargs):
phone = request.data.get('phone')
body = request.data.get('body')
...
对于 IN_QUERY 类型,像这样:
class CustomListView(ListAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = CustomSerializer
http_method_names = ['get']
@swagger_auto_schema(manual_parameters=[
openapi.Parameter('obj_id', openapi.IN_QUERY,
type=openapi.TYPE_STRING),
])
def get(self, request, *args, **kwargs):
obj_id = self.request.DATA.get('obj_id')