mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
Merge pull request #1543 from sinitsynsv/master
Add help_text and verbose_name attribute mapping for related field
This commit is contained in:
commit
1085b64fec
|
@ -828,6 +828,10 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
if model_field:
|
if model_field:
|
||||||
kwargs['required'] = not(model_field.null or model_field.blank)
|
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)
|
return PrimaryKeyRelatedField(**kwargs)
|
||||||
|
|
||||||
|
@ -1088,6 +1092,10 @@ class HyperlinkedModelSerializer(ModelSerializer):
|
||||||
|
|
||||||
if model_field:
|
if model_field:
|
||||||
kwargs['required'] = not(model_field.null or model_field.blank)
|
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:
|
if self.opts.lookup_field:
|
||||||
kwargs['lookup_field'] = self.opts.lookup_field
|
kwargs['lookup_field'] = self.opts.lookup_field
|
||||||
|
|
|
@ -143,7 +143,8 @@ class ForeignKeyTarget(RESTFrameworkModel):
|
||||||
|
|
||||||
class ForeignKeySource(RESTFrameworkModel):
|
class ForeignKeySource(RESTFrameworkModel):
|
||||||
name = models.CharField(max_length=100)
|
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
|
# Nullable ForeignKey
|
||||||
|
|
|
@ -9,7 +9,8 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework import serializers, fields, relations
|
from rest_framework import serializers, fields, relations
|
||||||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||||
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
|
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
|
from rest_framework.tests.models import BasicModelSerializer
|
||||||
import datetime
|
import datetime
|
||||||
import pickle
|
import pickle
|
||||||
|
@ -176,6 +177,16 @@ class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer):
|
||||||
fields = ['some_integer']
|
fields = ['some_integer']
|
||||||
|
|
||||||
|
|
||||||
|
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
|
class HyperlinkedForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
class BasicTests(TestCase):
|
class BasicTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.comment = Comment(
|
self.comment = Comment(
|
||||||
|
@ -1600,6 +1611,19 @@ class ManyFieldHelpTextTest(TestCase):
|
||||||
self.assertEqual('Some help text.', rel_field.help_text)
|
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')
|
@unittest.skipUnless(PIL is not None, 'PIL is not installed')
|
||||||
class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
|
class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user