mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Add help_text and verbose_name attribute mapping for related field
This commit is contained in:
parent
1d404874b3
commit
a6e525cf3a
|
@ -828,6 +828,10 @@ class ModelSerializer(Serializer):
|
|||
|
||||
if model_field:
|
||||
kwargs['required'] = not(model_field.null or model_field.blank)
|
||||
if model_field.help_text is not None:
|
||||
kwargs['help_text'] = model_field.help_text
|
||||
if model_field.verbose_name is not None:
|
||||
kwargs['label'] = model_field.verbose_name
|
||||
|
||||
return PrimaryKeyRelatedField(**kwargs)
|
||||
|
||||
|
@ -1088,6 +1092,10 @@ class HyperlinkedModelSerializer(ModelSerializer):
|
|||
|
||||
if model_field:
|
||||
kwargs['required'] = not(model_field.null or model_field.blank)
|
||||
if model_field.help_text is not None:
|
||||
kwargs['help_text'] = model_field.help_text
|
||||
if model_field.verbose_name is not None:
|
||||
kwargs['label'] = model_field.verbose_name
|
||||
|
||||
if self.opts.lookup_field:
|
||||
kwargs['lookup_field'] = self.opts.lookup_field
|
||||
|
|
|
@ -143,7 +143,8 @@ class ForeignKeyTarget(RESTFrameworkModel):
|
|||
|
||||
class ForeignKeySource(RESTFrameworkModel):
|
||||
name = models.CharField(max_length=100)
|
||||
target = models.ForeignKey(ForeignKeyTarget, related_name='sources')
|
||||
target = models.ForeignKey(ForeignKeyTarget, related_name='sources',
|
||||
help_text='Target', verbose_name='Target')
|
||||
|
||||
|
||||
# Nullable ForeignKey
|
||||
|
|
|
@ -9,7 +9,8 @@ from django.utils.translation import ugettext_lazy as _
|
|||
from rest_framework import serializers, fields, relations
|
||||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel)
|
||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel,
|
||||
ForeignKeySource, ManyToManySource)
|
||||
from rest_framework.tests.models import BasicModelSerializer
|
||||
import datetime
|
||||
import pickle
|
||||
|
@ -176,6 +177,16 @@ class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer):
|
|||
fields = ['some_integer']
|
||||
|
||||
|
||||
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ForeignKeySource
|
||||
|
||||
|
||||
class HyperlinkedForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = ForeignKeySource
|
||||
|
||||
|
||||
class BasicTests(TestCase):
|
||||
def setUp(self):
|
||||
self.comment = Comment(
|
||||
|
@ -1600,6 +1611,19 @@ class ManyFieldHelpTextTest(TestCase):
|
|||
self.assertEqual('Some help text.', rel_field.help_text)
|
||||
|
||||
|
||||
class AttributeMappingOnAutogeneratedRelatedFields(TestCase):
|
||||
|
||||
def test_primary_key_related_field(self):
|
||||
serializer = ForeignKeySourceSerializer()
|
||||
self.assertEqual(serializer.fields['target'].help_text, 'Target')
|
||||
self.assertEqual(serializer.fields['target'].label, 'Target')
|
||||
|
||||
def test_hyperlinked_related_field(self):
|
||||
serializer = HyperlinkedForeignKeySourceSerializer()
|
||||
self.assertEqual(serializer.fields['target'].help_text, 'Target')
|
||||
self.assertEqual(serializer.fields['target'].label, 'Target')
|
||||
|
||||
|
||||
@unittest.skipUnless(PIL is not None, 'PIL is not installed')
|
||||
class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user