From 760b25bc20a1434cbdd69dc0b13bacdc3bbedd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Fri, 30 Jan 2015 11:36:03 -0400 Subject: [PATCH 1/4] Fix AttributeError on renamed _field_mapping --- rest_framework/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index a3b8196bd2..a91fe23e02 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1330,13 +1330,13 @@ def get_unique_for_date_validators(self): if hasattr(models, 'UUIDField'): - ModelSerializer._field_mapping[models.UUIDField] = UUIDField + ModelSerializer.serializer_field_mapping[models.UUIDField] = UUIDField if postgres_fields: class CharMappingField(DictField): child = CharField() - ModelSerializer._field_mapping[postgres_fields.HStoreField] = CharMappingField + ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = CharMappingField class HyperlinkedModelSerializer(ModelSerializer): From 37dce89354ab2c94fefeb0a20b6265fef98caddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Sun, 1 Feb 2015 15:33:34 -0400 Subject: [PATCH 2/4] =?UTF-8?q?Add=20support=20for=20Django=201.8=E2=80=99?= =?UTF-8?q?s=20ArrayField?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rest_framework/serializers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index a91fe23e02..520b977487 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1337,6 +1337,7 @@ class CharMappingField(DictField): child = CharField() ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = CharMappingField + ModelSerializer.serializer_field_mapping[postgres_fields.ArrayField] = ListField class HyperlinkedModelSerializer(ModelSerializer): From 48fa77c09e2198c7877a724a46230caedcc7b529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Wed, 4 Feb 2015 23:33:59 -0400 Subject: [PATCH 3/4] Add child to ListField when using ArrayField --- rest_framework/serializers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 520b977487..84e4961b90 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -986,15 +986,25 @@ def build_standard_field(self, field_name, model_field): # Fields with choices get coerced into `ChoiceField` # instead of using their regular typed field. field_class = ChoiceField + if not issubclass(field_class, ModelField): # `model_field` is only valid for the fallback case of # `ModelField`, which is used when no other typed field # matched to the model field. field_kwargs.pop('model_field', None) + if not issubclass(field_class, CharField) and not issubclass(field_class, ChoiceField): # `allow_blank` is only valid for textual fields. field_kwargs.pop('allow_blank', None) + if postgres_fields and isinstance(model_field, postgres_fields.ArrayField): + child_model_field = model_field.base_field.base_field + child_field_class, child_field_kwargs = self.build_standard_field( + 'child', child_model_field + ) + + field_kwargs['child'] = child_field_class(**child_field_kwargs) + return field_class, field_kwargs def build_relational_field(self, field_name, relation_info): From c696b0ba0ced9527c8f4ad1bf6f71546d8fa65c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Thu, 5 Feb 2015 10:12:14 -0400 Subject: [PATCH 4/4] Fix possible nested array fields --- rest_framework/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 84e4961b90..1882195834 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -998,7 +998,7 @@ def build_standard_field(self, field_name, model_field): field_kwargs.pop('allow_blank', None) if postgres_fields and isinstance(model_field, postgres_fields.ArrayField): - child_model_field = model_field.base_field.base_field + child_model_field = model_field.base_field child_field_class, child_field_kwargs = self.build_standard_field( 'child', child_model_field )