Uro*_*ros 4 django django-rest-framework
我有带有 @action 装饰器的自定义函数,用于使用 GET 和 DELETE 两种方法进行路由。
如果代码位于同一函数中,则一切正常,并且我可以使用简单的 if 运行不同的操作:
@action(methods=['GET', 'DELETE'], detail=False, url_path='current', url_name='profile-current',
permission_classes=[IsAuthenticated])
def get_current_profile(self, request: Request, **kwargs) -> Response:
if self.request.method == 'DELETE':
...
Run Code Online (Sandbox Code Playgroud)
但是我想将代码分成两个仍然具有相同路线但不同方法的函数。
如果我将代码分成两个函数和相同的 url 路径和不同的方法,其中一个方法将返回方法不可用错误。
我是否在这里遗漏了一些东西,或者无法按照我认为应该工作的方式创建方法。
您可以尝试像这样拆分这两种方法
获取方法
@action(
methods=['GET'],
detail=False,
url_path='current',
url_name='profile-current',
permission_classes=[IsAuthenticated]
)
def get_current_profile(self, request: Request, **kwargs):
"""Get current profile"""
Run Code Online (Sandbox Code Playgroud)
删除方法
@get_current_profile.mapping.delete
def delete_current_profile(self, request, **kwargs):
"""Delete current profile"""
Run Code Online (Sandbox Code Playgroud)
链接在这里:文档
| 归档时间: |
|
| 查看次数: |
1987 次 |
| 最近记录: |