如何将自定义架构传递给drf detail_route?

时间:2017-10-31 17:31:31

标签: python django django-rest-framework

我有以下路线:

@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):
    # ...

当我转到显示我的架构的视图时,我得到:

我收到以下错误:

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.)"

1 个答案:

答案 0 :(得分:5)

在@ 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]+)'
)