django - 如何隐藏自动生成的模式中的字段,或明确定义模式?
问题描述
我正在使用drf-yasg
(Django REST Framework - Yet Another Swagger Generator)为我的 RESTful API 生成文档,但它并没有完全符合我的要求。我认为设置read_only
和write_only
属性 toTrue
会隐藏文档中的字段,因为它们在请求和响应主体中被省略,但事实并非如此。我没有看到任何在装饰器中定义模式的例子,甚至只是隐藏一个字段,但如果我能学会如何做其中的一件事情,我会处于良好状态。
让我们来看一个基本的例子:用户登录。
# serializers.py
class TokensSerializer(serializers.Serializer):
"""
Serializes access and refresh tokens for responses to a logged-in user.
"""
username = serializers.CharField(max_length=64, write_only=True)
access = serializers.CharField(max_length=4096, read_only=True)
refresh = serializers.CharField(max_length=4096, read_only=True)
# ...
class LoginSerializer(serializers.ModelSerializer):
"""
Serializes username, email, password, and tokens to allow for logging in.
"""
class Meta():
model = User
fields = ['username', 'email', 'password', 'tokens']
username = serializers.CharField(max_length=64)
email = serializers.CharField(max_length=254, read_only=True)
password = serializers.CharField(
min_length=8, max_length=64, write_only=True)
tokens = TokensSerializer(read_only=True)
# ...
这些序列化器分别生成Tokens
和Login
建模,分别以以下.json和.yaml格式定义:
{
"definitions": {
"Tokens": {
"required": ["username"],
"type": "object",
"properties": {
"username": {
"title": "Username",
"type": "string",
"maxLength": 64,
"minLength": 1
},
"access": {
"title": "Access",
"type": "string",
"readOnly": true,
"maxLength": 4096,
"minLength": 1
},
"refresh": {
"title": "Refresh",
"type": "string",
"readOnly": true,
"maxLength": 4096,
"minLength": 1
}
}
},
"Login": {
"required": ["username", "password"],
"type": "object",
"properties": {
"username": {
"title": "Username",
"type": "string",
"maxLength": 64,
"minLength": 1
},
"email": {
"title": "Email",
"type": "string",
"readOnly": true,
"maxLength": 254,
"minLength": 1
},
"password": {
"title": "Password",
"type": "string",
"maxLength": 64,
"minLength": 8
},
"tokens": {
"$ref": "#/definitions/Tokens"
}
}
}
}
}
definitions:
Tokens:
required:
- username
type: object
properties:
username:
title: Username
type: string
maxLength: 64
minLength: 1
access:
title: Access
type: string
readOnly: true
maxLength: 4096
minLength: 1
refresh:
title: Refresh
type: string
readOnly: true
maxLength: 4096
minLength: 1
Login:
required:
- username
- password
type: object
properties:
username:
title: Username
type: string
maxLength: 64
minLength: 1
email:
title: Email
type: string
readOnly: true
maxLength: 254
minLength: 1
password:
title: Password
type: string
maxLength: 64
minLength: 8
tokens:
$ref: '#/definitions/Tokens'
但是,这些并不适合每个请求或响应正文,我想明确定义模式,或者使用省略字段的现有模式。我不知道如何实现后者,但这是我对前者的尝试,这会导致错误:
class LoginView(GenericAPIView):
"""
View for taking in an existing user's credentials and authorizing them if valid or denying access if invalid.
"""
serializer_class = LoginSerializer
@swagger_auto_schema(
responses={
201: {
'schema': {
'description': 'TODO',
'required': ['username', 'email', 'tokens'],
'type': 'object',
'properties': {
'username': {
'title': 'Username',
'type': 'string',
'maxLength': 64,
'minLength': 1
},
'email': {
'title': 'Email',
'type': 'string',
'readOnly': True,
'maxLength': 254,
'minLength': 1
},
'tokens': {
'$ref': '#/definitions/Tokens'
}
}
}
}
})
def post(self, request):
# ...
Failed to load API definition.
Errors
Fetch error
Internal Server Error http://localhost:8000/api/v0-alpha/?format=openapi
我能想到的唯一其他选择是为每个模式创建一个序列化程序,但这似乎比必要的更费力。有什么想法吗?
解决方案
您可以将ref_name
嵌套序列化程序的 Meta 类设置None
为此处回答:
https ://github.com/axnsan12/drf-yasg/issues/239#issuecomment-442629230
推荐阅读
- java - 为什么 List.contains(Object) 的行为不同?
- r - 如何在同一个数据帧上做colsum和average
- powerbi - 使用 DAX 生成日期系列
- javascript - 仅在服务器上需要一个包
- java - Intellij IDEA 仅针对所有未提交的更改运行测试
- regex - 在换行符之前匹配字符,不包括空格?
- macos - MacOS - 使用带有launchd的脚本 - 启动,登录,注销,关闭?
- service-worker - Workbox 的服务人员在更改时未更新
- python - AllenNLP 共指分辨率的多 GPU 训练
- reactjs - 事件处理程序中带有 [name] 的 PrevState