mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 04:20:12 +03:00
Added the test case to report if we span more than one relation.
This commit is contained in:
parent
de0a072d5a
commit
d2ded0ddad
|
@ -957,7 +957,7 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
# Raise an error if we span more than one relation
|
# Raise an error if we span more than one relation
|
||||||
if len(keys) > 2:
|
if len(keys) > 2:
|
||||||
self._errors[key] = 'Can not span more than a relation'
|
self._errors[key] = 'Can not span more than a relation.'
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Mark the related instance as the one to save
|
# Mark the related instance as the one to save
|
||||||
|
|
|
@ -1889,3 +1889,19 @@ class RelationSpanningSerializerTest(TestCase):
|
||||||
serializer = TicketSerializer(ticket, data={'username': 'doe'})
|
serializer = TicketSerializer(ticket, data={'username': 'doe'})
|
||||||
self.assertFalse(serializer.is_valid())
|
self.assertFalse(serializer.is_valid())
|
||||||
self.assertEqual(serializer.errors, {'username': 'Related object does not exist.'})
|
self.assertEqual(serializer.errors, {'username': 'Related object does not exist.'})
|
||||||
|
|
||||||
|
def test_multiple_model_traversal_update(self):
|
||||||
|
"""Update a field through a foreign key during an update."""
|
||||||
|
class TicketSerializer(serializers.ModelSerializer):
|
||||||
|
username = fields.CharField(source='assigned.demo.name')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Ticket
|
||||||
|
fields = ('username',)
|
||||||
|
|
||||||
|
owner = Person.objects.create(name='john')
|
||||||
|
reviewer = Person.objects.create(name='reviewer')
|
||||||
|
ticket = Ticket.objects.create(assigned=owner, reviewer=reviewer)
|
||||||
|
serializer = TicketSerializer(ticket, data={'username': 'doe'})
|
||||||
|
self.assertFalse(serializer.is_valid())
|
||||||
|
self.assertEqual(serializer.errors, {'username': 'Can not span more than a relation.'})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user