From aa72f8d63d2a7b33a2e74eaba216b56c803af70c Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 17 Dec 2012 21:59:51 +0000 Subject: [PATCH] Fix bug with M2M in browseable API --- rest_framework/renderers.py | 2 +- rest_framework/serializers.py | 10 +++------- rest_framework/tests/generics.py | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 1220bca10..a4ae717db 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -20,7 +20,7 @@ from rest_framework.utils import dict2xml from rest_framework.utils import encoders from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework import VERSION, status -from rest_framework import serializers, parsers +from rest_framework import parsers class BaseRenderer(object): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 1d93f7775..02377492c 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -160,6 +160,9 @@ class BaseSerializer(Field): for key in self.opts.exclude: ret.pop(key, None) + for key, field in ret.items(): + field.initialize(parent=self, field_name=key) + return ret ##### @@ -174,13 +177,6 @@ class BaseSerializer(Field): if parent.opts.depth: self.opts.depth = parent.opts.depth - 1 - # We need to call initialize here to ensure any nested - # serializers that will have already called initialize on their - # descendants get updated with *their* parent. - # We could be a bit more smart about this, but it'll do for now. - for key, field in self.fields.items(): - field.initialize(parent=self, field_name=key) - ##### # Methods to convert or revert from objects <--> primitive representations. diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py index a8279ef2b..14fa66f93 100644 --- a/rest_framework/tests/generics.py +++ b/rest_framework/tests/generics.py @@ -1,3 +1,4 @@ +from django.db import models from django.test import TestCase from django.test.client import RequestFactory from django.utils import simplejson as json @@ -301,3 +302,36 @@ class TestCreateModelWithAutoNowAddField(TestCase): self.assertEquals(response.status_code, status.HTTP_201_CREATED) created = self.objects.get(id=1) self.assertEquals(created.content, 'foobar') + + +# Test for particularly ugly reression with m2m in browseable API +class ClassB(models.Model): + name = models.CharField(max_length=255) + + +class ClassA(models.Model): + name = models.CharField(max_length=255) + childs = models.ManyToManyField(ClassB, blank=True, null=True) + + +class ClassASerializer(serializers.ModelSerializer): + childs = serializers.ManyPrimaryKeyRelatedField(source='childs') + + class Meta: + model = ClassA + + +class ExampleView(generics.ListCreateAPIView): + serializer_class = ClassASerializer + model = ClassA + + +class TestM2MBrowseableAPI(TestCase): + def test_m2m_in_browseable_api(self): + """ + Test for particularly ugly reression with m2m in browseable API + """ + request = factory.get('/', HTTP_ACCEPT='text/html') + view = ExampleView().as_view() + response = view(request).render() + self.assertEquals(response.status_code, status.HTTP_200_OK)