Commit aa72f8d6 by Tom Christie

Fix bug with M2M in browseable API

parent 70714c23
...@@ -20,7 +20,7 @@ from rest_framework.utils import dict2xml ...@@ -20,7 +20,7 @@ from rest_framework.utils import dict2xml
from rest_framework.utils import encoders from rest_framework.utils import encoders
from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework import VERSION, status from rest_framework import VERSION, status
from rest_framework import serializers, parsers from rest_framework import parsers
class BaseRenderer(object): class BaseRenderer(object):
......
...@@ -160,6 +160,9 @@ class BaseSerializer(Field): ...@@ -160,6 +160,9 @@ class BaseSerializer(Field):
for key in self.opts.exclude: for key in self.opts.exclude:
ret.pop(key, None) ret.pop(key, None)
for key, field in ret.items():
field.initialize(parent=self, field_name=key)
return ret return ret
##### #####
...@@ -174,13 +177,6 @@ class BaseSerializer(Field): ...@@ -174,13 +177,6 @@ class BaseSerializer(Field):
if parent.opts.depth: if parent.opts.depth:
self.opts.depth = parent.opts.depth - 1 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. # Methods to convert or revert from objects <--> primitive representations.
......
from django.db import models
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.utils import simplejson as json from django.utils import simplejson as json
...@@ -301,3 +302,36 @@ class TestCreateModelWithAutoNowAddField(TestCase): ...@@ -301,3 +302,36 @@ class TestCreateModelWithAutoNowAddField(TestCase):
self.assertEquals(response.status_code, status.HTTP_201_CREATED) self.assertEquals(response.status_code, status.HTTP_201_CREATED)
created = self.objects.get(id=1) created = self.objects.get(id=1)
self.assertEquals(created.content, 'foobar') 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment