Skip to content

Commit

Permalink
Merge pull request #5 from evenicoulddoit/fix-nested-non-model-serial…
Browse files Browse the repository at this point in the history
…izers

Fix implicit ModelSerializer assumption for expanded fields
  • Loading branch information
evenicoulddoit authored Apr 11, 2017
2 parents 4db144f + 9c1a3a5 commit f3510fc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
### 0.5.0
### 0.5.1 (2017-04-11)
* Bugfix to support expanding non-model serializers

### 0.5.0 (2017-01-20)
* Added `read_only=False` field definition support
2 changes: 1 addition & 1 deletion rest_framework_serializer_extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.5.0'
__version__ = '0.5.1'
5 changes: 4 additions & 1 deletion rest_framework_serializer_extensions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def _standardise_expandable_definition(self, definition):
return definition

# The model used for the HashId (defaults to the serializer's model)
if 'id_model' not in definition:
if (
definition.get('id_source') is not False and
'id_model' not in definition
):
definition['id_model'] = definition['serializer'].Meta.model

# read_only defaults to True
Expand Down
66 changes: 66 additions & 0 deletions tests/test_serializers__expandable_fields_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,29 @@ class Meta:
)


class NonModelTestSerializer(ExpandableFieldsMixin, serializers.Serializer):
is_model = serializers.SerializerMethodField()

def get_is_model(self, obj):
return False

def get_sku(self, obj):
sku = models.Sku.objects.get(variant='P100D')
serializer = SkuTestSerializer(instance=sku, context=self.context)
serializer.bind('sku', self)
return serializer.data

class Meta:
expandable_fields = dict(
non_model=dict(
serializer='{0}.NonModelTestSerializer'.format(MODULE),
source='*',
id_source=False
),
sku=serializers.SerializerMethodField
)


"""
END TEST SERIALIZERS
"""
Expand Down Expand Up @@ -742,6 +765,49 @@ def test_read_only_field_not_required(self):
serializer = CarModelTestSerializer(data=dict(name='Ka'))
self.assertTrue(serializer.is_valid())

def test_unexpanded_non_model_serializer(self):
"""
Non-model serializers should support the mixin.
"""
serializer = NonModelTestSerializer({})
self.assertDictEqual(serializer.data, dict(is_model=False))

def test_expanded_non_model_serializer_with_non_model_serializer(self):
"""
Non-model to non-model expansion should work as expected.
"""
serializer = NonModelTestSerializer(
{}, context=dict(expand={'non_model'})
)
self.assertDictEqual(
serializer.data,
dict(
is_model=False,
non_model=dict(
is_model=False
)
)
)

def test_expanded_non_model_serializer_with_model_serializer(self):
"""
Non-model to model expansion should work as expected.
"""
serializer = NonModelTestSerializer(
{}, context=dict(expand={'sku'})
)
self.assertDictEqual(
serializer.data,
dict(
is_model=False,
sku=dict(
id=self.sku_p100d.pk,
variant=self.sku_p100d.variant,
model_id=self.expand_instance_id(self.carmodel_model_s)
)
)
)


@override_settings(
REST_FRAMEWORK=dict(
Expand Down

0 comments on commit f3510fc

Please sign in to comment.