mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
Add DefaultSchema utility
This commit is contained in:
parent
4b09e82402
commit
f4d62c5467
|
@ -23,7 +23,7 @@ Other access should target the submodules directly
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
|
|
||||||
from .generators import SchemaGenerator
|
from .generators import SchemaGenerator
|
||||||
from .inspectors import AutoSchema, ManualSchema # noqa
|
from .inspectors import AutoSchema, DefaultSchema, ManualSchema # noqa
|
||||||
|
|
||||||
|
|
||||||
def get_schema_view(
|
def get_schema_view(
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
inspectors.py # Per-endpoint view introspection
|
inspectors.py # Per-endpoint view introspection
|
||||||
|
|
||||||
|
@ -456,3 +457,13 @@ class ManualSchema(ViewInspector):
|
||||||
)
|
)
|
||||||
|
|
||||||
return self._link
|
return self._link
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultSchema(object):
|
||||||
|
"""Allows overriding AutoSchema in with DEFAULT_SCHEMA_CLASS setting"""
|
||||||
|
def __get__(self, instance, owner):
|
||||||
|
inspector_class = api_settings.DEFAULT_SCHEMA_CLASS
|
||||||
|
assert issubclass(inspector_class, ViewInspector), "DEFAULT_SCHEMA_CLASS must be set to a ViewInspector (usually an AutoSchema) subclass"
|
||||||
|
inspector = inspector_class()
|
||||||
|
inspector.view = instance
|
||||||
|
return inspector
|
||||||
|
|
|
@ -55,6 +55,9 @@ DEFAULTS = {
|
||||||
'DEFAULT_PAGINATION_CLASS': None,
|
'DEFAULT_PAGINATION_CLASS': None,
|
||||||
'DEFAULT_FILTER_BACKENDS': (),
|
'DEFAULT_FILTER_BACKENDS': (),
|
||||||
|
|
||||||
|
# Schema
|
||||||
|
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
|
||||||
|
|
||||||
# Throttling
|
# Throttling
|
||||||
'DEFAULT_THROTTLE_RATES': {
|
'DEFAULT_THROTTLE_RATES': {
|
||||||
'user': None,
|
'user': None,
|
||||||
|
@ -140,6 +143,7 @@ IMPORT_STRINGS = (
|
||||||
'DEFAULT_VERSIONING_CLASS',
|
'DEFAULT_VERSIONING_CLASS',
|
||||||
'DEFAULT_PAGINATION_CLASS',
|
'DEFAULT_PAGINATION_CLASS',
|
||||||
'DEFAULT_FILTER_BACKENDS',
|
'DEFAULT_FILTER_BACKENDS',
|
||||||
|
'DEFAULT_SCHEMA_CLASS',
|
||||||
'EXCEPTION_HANDLER',
|
'EXCEPTION_HANDLER',
|
||||||
'TEST_REQUEST_RENDERER_CLASSES',
|
'TEST_REQUEST_RENDERER_CLASSES',
|
||||||
'UNAUTHENTICATED_USER',
|
'UNAUTHENTICATED_USER',
|
||||||
|
|
|
@ -18,7 +18,7 @@ from django.views.generic import View
|
||||||
from rest_framework import exceptions, status
|
from rest_framework import exceptions, status
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.schemas import AutoSchema
|
from rest_framework.schemas import DefaultSchema
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils import formatting
|
from rest_framework.utils import formatting
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ class APIView(View):
|
||||||
# Allow dependency injection of other settings to make testing easier.
|
# Allow dependency injection of other settings to make testing easier.
|
||||||
settings = api_settings
|
settings = api_settings
|
||||||
|
|
||||||
schema = AutoSchema()
|
schema = DefaultSchema()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def as_view(cls, **initkwargs):
|
def as_view(cls, **initkwargs):
|
||||||
|
|
|
@ -536,7 +536,7 @@ class TestAutoSchema(TestCase):
|
||||||
assert isinstance(view.schema, CustomViewInspector)
|
assert isinstance(view.schema, CustomViewInspector)
|
||||||
|
|
||||||
def test_set_custom_inspector_class_via_settings(self):
|
def test_set_custom_inspector_class_via_settings(self):
|
||||||
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS':'tests.test_schemas.CustomViewInspector'}):
|
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'tests.test_schemas.CustomViewInspector'}):
|
||||||
view = APIView()
|
view = APIView()
|
||||||
assert isinstance(view.schema, CustomViewInspector)
|
assert isinstance(view.schema, CustomViewInspector)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user