diff --git a/rest_framework/metadata.py b/rest_framework/metadata.py index 580259de1..af4bc396b 100644 --- a/rest_framework/metadata.py +++ b/rest_framework/metadata.py @@ -10,7 +10,6 @@ from __future__ import unicode_literals from django.core.exceptions import PermissionDenied from django.http import Http404 -from django.utils import six from django.utils.datastructures import SortedDict from rest_framework import exceptions, serializers from rest_framework.compat import force_text @@ -100,7 +99,7 @@ class SimpleMetadata(BaseMetadata): """ return SortedDict([ (field_name, self.get_field_info(field)) - for field_name, field in six.iteritems(serializer.fields) + for field_name, field in serializer.fields.items() ]) def get_field_info(self, field): diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index c5a9270a4..fb4512854 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -72,7 +72,6 @@ class BasePaginationSerializer(serializers.Serializer): child=object_serializer(), source='object_list' ) - self.fields[results_field].bind(results_field, self, self) class PaginationSerializer(BasePaginationSerializer): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8902294bc..12e380900 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -149,6 +149,28 @@ class SerializerMetaclass(type): return super(SerializerMetaclass, cls).__new__(cls, name, bases, attrs) +class BindingDict(object): + def __init__(self, serializer): + self.serializer = serializer + self.fields = SortedDict() + + def __setitem__(self, key, field): + self.fields[key] = field + field.bind(field_name=key, parent=self.serializer, root=self.serializer) + + def __getitem__(self, key): + return self.fields[key] + + def __delitem__(self, key): + del self.fields[key] + + def items(self): + return self.fields.items() + + def values(self): + return self.fields.values() + + @six.add_metaclass(SerializerMetaclass) class Serializer(BaseSerializer): def __init__(self, *args, **kwargs): @@ -161,11 +183,9 @@ class Serializer(BaseSerializer): # Every new serializer is created with a clone of the field instances. # This allows users to dynamically modify the fields on a serializer # instance without affecting every other serializer class. - self.fields = self._get_base_fields() - - # Setup all the child fields, to provide them with the current context. - for field_name, field in self.fields.items(): - field.bind(field_name, self, self) + self.fields = BindingDict(self) + for key, value in self._get_base_fields().items(): + self.fields[key] = value def __new__(cls, *args, **kwargs): # We override this method in order to automagically create