This commit is contained in:
Matías Lang 2017-05-26 16:30:53 +00:00 committed by GitHub
commit 3a090af6c8
2 changed files with 11 additions and 2 deletions

View File

@ -1236,7 +1236,11 @@ class ModelSerializer(Serializer):
Create a read only field for model methods and properties.
"""
field_class = ReadOnlyField
field_kwargs = {}
func = getattr(model_class, field_name)
if func.__doc__:
field_kwargs = {'help_text': func.__doc__}
else:
field_kwargs = {}
return field_class, field_kwargs

View File

@ -71,6 +71,10 @@ class RegularFieldsModel(models.Model):
def method(self):
return 'method'
def docstring_method(self):
"""test"""
return 'method'
COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green'))
DECIMAL_CHOICES = (('low', decimal.Decimal('0.1')), ('medium', decimal.Decimal('0.5')), ('high', decimal.Decimal('0.9')))
@ -223,12 +227,13 @@ class TestRegularFieldMappings(TestCase):
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = RegularFieldsModel
fields = ('auto_field', 'method')
fields = ('auto_field', 'method', 'docstring_method')
expected = dedent("""
TestSerializer():
auto_field = IntegerField(read_only=True)
method = ReadOnlyField()
docstring_method = ReadOnlyField(help_text='test')
""")
self.assertEqual(repr(TestSerializer()), expected)