From fc0d55000d335ad19acba4cdafc7a4f4b1b02bd1 Mon Sep 17 00:00:00 2001 From: Ignat Shining Date: Tue, 15 Aug 2017 17:00:06 +0300 Subject: [PATCH 1/2] Schema overriding support --- rest_framework/fields.py | 5 ++++- rest_framework/schemas.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 9cc9ab03f..eaea8c871 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -309,7 +309,7 @@ class Field(object): def __init__(self, read_only=False, write_only=False, required=None, default=empty, initial=empty, source=None, - label=None, help_text=None, style=None, + label=None, help_text=None, schema=None, style=None, error_messages=None, validators=None, allow_null=False): self._creation_counter = Field._creation_counter Field._creation_counter += 1 @@ -342,6 +342,9 @@ class Field(object): if validators is not None: self.validators = validators[:] + if schema is not None: + self.schema = schema + # These are set up by `.bind()` when the field is added to a serializer. self.field_name = None self.parent = None diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 875f9454b..37b7ce302 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -27,6 +27,9 @@ header_regex = re.compile('^[a-zA-Z][0-9A-Za-z_]*:') def field_to_schema(field): + if hasattr(field, 'schema'): + return field.schema + title = force_text(field.label) if field.label else '' description = force_text(field.help_text) if field.help_text else '' From 8ff567a06a4600391c158f3ec977211899729cbe Mon Sep 17 00:00:00 2001 From: Ignat Shining Date: Tue, 15 Aug 2017 17:40:47 +0300 Subject: [PATCH 2/2] Add schema overriding support for coerced fields --- rest_framework/relations.py | 2 +- rest_framework/serializers.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 4d3bdba1d..9a4b291b3 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -75,7 +75,7 @@ class PKOnlyObject(object): # rather than the parent serializer. MANY_RELATION_KWARGS = ( 'read_only', 'write_only', 'required', 'default', 'initial', 'source', - 'label', 'help_text', 'style', 'error_messages', 'allow_empty', + 'label', 'help_text', 'schema', 'style', 'error_messages', 'allow_empty', 'html_cutoff', 'html_cutoff_text' ) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index a4b51ae9d..04818b204 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -74,7 +74,7 @@ from rest_framework.relations import Hyperlink, PKOnlyObject # NOQA # isort:ski # rather than the parent serializer. LIST_SERIALIZER_KWARGS = ( 'read_only', 'write_only', 'required', 'default', 'initial', 'source', - 'label', 'help_text', 'style', 'error_messages', 'allow_empty', + 'label', 'help_text', 'schema', 'style', 'error_messages', 'allow_empty', 'instance', 'data', 'partial', 'context', 'allow_null' ) @@ -1174,7 +1174,7 @@ class ModelSerializer(Serializer): valid_kwargs = set(( 'read_only', 'write_only', 'required', 'default', 'initial', 'source', - 'label', 'help_text', 'style', + 'label', 'help_text', 'schema', 'style', 'error_messages', 'validators', 'allow_null', 'allow_blank', 'choices' ))