mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Use the help_text attribute of serializer fields in the schema doc
This commit is contained in:
parent
e407dc7f01
commit
99aa001f10
|
@ -4,6 +4,7 @@ from django.conf import settings
|
||||||
from django.contrib.admindocs.views import simplify_regex
|
from django.contrib.admindocs.views import simplify_regex
|
||||||
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
|
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
from rest_framework import exceptions, serializers
|
from rest_framework import exceptions, serializers
|
||||||
from rest_framework.compat import coreapi, uritemplate, urlparse
|
from rest_framework.compat import coreapi, uritemplate, urlparse
|
||||||
|
@ -258,8 +259,6 @@ class SchemaGenerator(object):
|
||||||
if not hasattr(view, 'get_serializer_class'):
|
if not hasattr(view, 'get_serializer_class'):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
fields = []
|
|
||||||
|
|
||||||
serializer_class = view.get_serializer_class()
|
serializer_class = view.get_serializer_class()
|
||||||
serializer = serializer_class()
|
serializer = serializer_class()
|
||||||
|
|
||||||
|
@ -269,11 +268,17 @@ class SchemaGenerator(object):
|
||||||
if not isinstance(serializer, serializers.Serializer):
|
if not isinstance(serializer, serializers.Serializer):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
fields = []
|
||||||
for field in serializer.fields.values():
|
for field in serializer.fields.values():
|
||||||
if field.read_only:
|
if field.read_only:
|
||||||
continue
|
continue
|
||||||
required = field.required and method != 'PATCH'
|
required = field.required and method != 'PATCH'
|
||||||
field = coreapi.Field(name=field.source, location='form', required=required)
|
field = coreapi.Field(
|
||||||
|
name=field.source,
|
||||||
|
location='form',
|
||||||
|
required=required,
|
||||||
|
description=force_text(field.help_text),
|
||||||
|
)
|
||||||
fields.append(field)
|
fields.append(field)
|
||||||
|
|
||||||
return fields
|
return fields
|
||||||
|
|
|
@ -23,8 +23,8 @@ class ExamplePagination(pagination.PageNumberPagination):
|
||||||
|
|
||||||
|
|
||||||
class ExampleSerializer(serializers.Serializer):
|
class ExampleSerializer(serializers.Serializer):
|
||||||
a = serializers.CharField(required=True)
|
a = serializers.CharField(required=True, help_text='About a')
|
||||||
b = serializers.CharField(required=False)
|
b = serializers.CharField(required=False, help_text='About b')
|
||||||
|
|
||||||
|
|
||||||
class ExampleViewSet(ModelViewSet):
|
class ExampleViewSet(ModelViewSet):
|
||||||
|
@ -109,8 +109,8 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
action='post',
|
action='post',
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('a', required=True, location='form'),
|
coreapi.Field('a', required=True, location='form', description='About a'),
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form', description='About b')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'retrieve': coreapi.Link(
|
'retrieve': coreapi.Link(
|
||||||
|
@ -126,8 +126,8 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
coreapi.Field('a', required=True, location='form'),
|
coreapi.Field('a', required=True, location='form', description='About a'),
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form', description='About b')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'partial_update': coreapi.Link(
|
'partial_update': coreapi.Link(
|
||||||
|
@ -136,8 +136,8 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('pk', required=True, location='path'),
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
coreapi.Field('a', required=False, location='form'),
|
coreapi.Field('a', required=False, location='form', description='About a'),
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form', description='About b')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'destroy': coreapi.Link(
|
'destroy': coreapi.Link(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user