gpt4 book ai didi

python - 如何将自定义模式传递给 drf detail_route?

转载 作者:太空宇宙 更新时间:2023-11-04 08:38:15 24 4
gpt4 key购买 nike

我有以下路线:

@detail_route(methods=['post'], permission_classes=[IsOperator], schema=AutoSchema(manual_fields=[
coreapi.Field('size', location='query', schema={'type': 'number', 'example': 1024}),
coreapi.Field('filename', location='query', schema={'type': 'string', 'example': 'telemetry.tlog'})
]))
def get_upload_url(self):
# ...

当我转到显示我的模式的 View 时,我得到:

我收到以下错误:

AttributeError: 'AutoSchema' object has no attribute '_view'

堆栈跟踪:

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
489. response = self.handle_exception(exc)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
449. self.raise_uncaught_exception(exc)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
486. response = handler(request, *args, **kwargs)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework_swagger/views.py" in get
32. schema = generator.get_schema(request=request)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/schemas/generators.py" in get_schema
279. links = self.get_links(None if public else request)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/schemas/generators.py" in get_links
317. link = view.schema.get_link(path, method, base_url=self.url)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/schemas/inspectors.py" in get_link
166. fields = self.get_path_fields(path, method)

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/schemas/inspectors.py" in get_path_fields
237. view = self.view

File "/Users/frederikcreemers/.virtualenvs/theproject/lib/python2.7/site-packages/rest_framework/schemas/inspectors.py" in view
123. assert self._view is not None, "Schema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view class rather than an instance.)"

最佳答案

在@list_route 和@details_route 中你可以这样添加

@list_route(
schema=ManualSchema(fields=[
coreapi.Field(
"page",
required=True,
location="query",
schema=coreschema.String()
),
coreapi.Field(
"page_size",
required=True,
location="query",
schema=coreschema.String()
),
coreapi.Field(
"product_id",
required=True,
location="path",
schema=coreschema.Integer()
)
]),
url_name='product',
url_path='product/(?P<product_id>[0-9]+)'
)

关于python - 如何将自定义模式传递给 drf detail_route?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com