mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Respect blank on many to many, and set allow_empty=False if it is not present. Closes #2804.
This commit is contained in:
parent
aa3f844b3d
commit
e68d737825
|
@ -64,6 +64,7 @@ class RelatedField(Field):
|
||||||
'when setting read_only=`True`.'
|
'when setting read_only=`True`.'
|
||||||
)
|
)
|
||||||
kwargs.pop('many', None)
|
kwargs.pop('many', None)
|
||||||
|
kwargs.pop('allow_empty', None)
|
||||||
super(RelatedField, self).__init__(**kwargs)
|
super(RelatedField, self).__init__(**kwargs)
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
|
|
|
@ -225,6 +225,7 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
# If this field is read-only, then return early.
|
# If this field is read-only, then return early.
|
||||||
# No further keyword arguments are valid.
|
# No further keyword arguments are valid.
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
if model_field.has_default() or model_field.null:
|
if model_field.has_default() or model_field.null:
|
||||||
kwargs['required'] = False
|
kwargs['required'] = False
|
||||||
if model_field.null:
|
if model_field.null:
|
||||||
|
@ -234,6 +235,8 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
if getattr(model_field, 'unique', False):
|
if getattr(model_field, 'unique', False):
|
||||||
validator = UniqueValidator(queryset=model_field.model._default_manager)
|
validator = UniqueValidator(queryset=model_field.model._default_manager)
|
||||||
kwargs['validators'] = kwargs.get('validators', []) + [validator]
|
kwargs['validators'] = kwargs.get('validators', []) + [validator]
|
||||||
|
if to_many and not model_field.blank:
|
||||||
|
kwargs['allow_empty'] = False
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
|
@ -395,7 +395,7 @@ class TestRelationalFieldMappings(TestCase):
|
||||||
id = IntegerField(label='ID', read_only=True)
|
id = IntegerField(label='ID', read_only=True)
|
||||||
foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all())
|
foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all())
|
||||||
one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all(), validators=[<UniqueValidator(queryset=RelationalModel.objects.all())>])
|
one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all(), validators=[<UniqueValidator(queryset=RelationalModel.objects.all())>])
|
||||||
many_to_many = PrimaryKeyRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all())
|
many_to_many = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=ManyToManyTargetModel.objects.all())
|
||||||
through = PrimaryKeyRelatedField(many=True, read_only=True)
|
through = PrimaryKeyRelatedField(many=True, read_only=True)
|
||||||
""")
|
""")
|
||||||
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
||||||
|
@ -434,7 +434,7 @@ class TestRelationalFieldMappings(TestCase):
|
||||||
url = HyperlinkedIdentityField(view_name='relationalmodel-detail')
|
url = HyperlinkedIdentityField(view_name='relationalmodel-detail')
|
||||||
foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail')
|
foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail')
|
||||||
one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), validators=[<UniqueValidator(queryset=RelationalModel.objects.all())>], view_name='onetoonetargetmodel-detail')
|
one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), validators=[<UniqueValidator(queryset=RelationalModel.objects.all())>], view_name='onetoonetargetmodel-detail')
|
||||||
many_to_many = HyperlinkedRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail')
|
many_to_many = HyperlinkedRelatedField(allow_empty=False, many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail')
|
||||||
through = HyperlinkedRelatedField(many=True, read_only=True, view_name='throughtargetmodel-detail')
|
through = HyperlinkedRelatedField(many=True, read_only=True, view_name='throughtargetmodel-detail')
|
||||||
""")
|
""")
|
||||||
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
self.assertEqual(unicode_repr(TestSerializer()), expected)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user