mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Merge 0135d7ca5e
into 26623436f3
This commit is contained in:
commit
3a090af6c8
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user