Merge pull request #1234 from linovia/bugfix/nested_models_without_related_name

Fixed the nested model serializers in case of the related_name isn’t set...
This commit is contained in:
Tom Christie 2013-12-13 03:06:14 -08:00
commit a129bdd0f0
2 changed files with 38 additions and 2 deletions

View File

@ -878,7 +878,7 @@ class ModelSerializer(Serializer):
# Reverse fk or one-to-one relations # Reverse fk or one-to-one relations
for (obj, model) in meta.get_all_related_objects_with_model(): for (obj, model) in meta.get_all_related_objects_with_model():
field_name = obj.field.related_query_name() field_name = obj.get_accessor_name()
if field_name in attrs: if field_name in attrs:
related_data[field_name] = attrs.pop(field_name) related_data[field_name] = attrs.pop(field_name)
@ -949,11 +949,12 @@ class ModelSerializer(Serializer):
del(obj._m2m_data) del(obj._m2m_data)
if getattr(obj, '_related_data', None): if getattr(obj, '_related_data', None):
related_fields = dict(((f.get_accessor_name(), f) for f, m in obj._meta.get_all_related_objects_with_model()))
for accessor_name, related in obj._related_data.items(): for accessor_name, related in obj._related_data.items():
if isinstance(related, RelationsList): if isinstance(related, RelationsList):
# Nested reverse fk relationship # Nested reverse fk relationship
for related_item in related: for related_item in related:
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name fk_field = related_fields[accessor_name].field.name
setattr(related_item, fk_field, obj) setattr(related_item, fk_field, obj)
self.save_object(related_item) self.save_object(related_item)

View File

@ -6,6 +6,7 @@ Doesn't cover model serializers.
from __future__ import unicode_literals from __future__ import unicode_literals
from django.test import TestCase from django.test import TestCase
from rest_framework import serializers from rest_framework import serializers
from . import models
class WritableNestedSerializerBasicTests(TestCase): class WritableNestedSerializerBasicTests(TestCase):
@ -311,3 +312,37 @@ class ForeignKeyNestedSerializerUpdateTests(TestCase):
serializer = self.AlbumSerializer(instance=original, data=data) serializer = self.AlbumSerializer(instance=original, data=data)
self.assertEqual(serializer.is_valid(), True) self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected) self.assertEqual(serializer.object, expected)
class NestedModelSerializerUpdateTests(TestCase):
def test_second_nested_level(self):
john = models.Person.objects.create(name="john")
post = john.blogpost_set.create(title="Test blog post")
post.blogpostcomment_set.create(text="I hate this blog post")
post.blogpostcomment_set.create(text="I love this blog post")
class BlogPostCommentSerializer(serializers.ModelSerializer):
class Meta:
model = models.BlogPostComment
class BlogPostSerializer(serializers.ModelSerializer):
comments = BlogPostCommentSerializer(many=True, source='blogpostcomment_set')
class Meta:
model = models.BlogPost
fields = ('id', 'title', 'comments')
class PersonSerializer(serializers.ModelSerializer):
posts = BlogPostSerializer(many=True, source='blogpost_set')
class Meta:
model = models.Person
fields = ('id', 'name', 'age', 'posts')
serialize = PersonSerializer(instance=john)
deserialize = PersonSerializer(data=serialize.data, instance=john)
self.assertTrue(deserialize.is_valid())
result = deserialize.object
result.save()
self.assertEqual(result.id, john.id)