evenicoulddoit / django-rest-framework-serializer-extensions

Extensions to help DRY up Django Rest Framework serializers
Other
247 stars 23 forks source link

There is an Implicit assumption that the serializer in an expandable field definition subclasses ModelSerializer #4

Closed gareth-lloyd closed 7 years ago

gareth-lloyd commented 7 years ago

To reproduce:

from rest_framework import serializers
from rest_framework_serializer_extensions import serializers as extension_serializers

class MyChildSerializer(serializers.Serializer):
    pass

class MyParentSerializer(extension_serializers.ExpandableFieldsMixin, serializers.Serializer):
    class Meta:
        expandable_fields = {
            "child": MyChildSerializer
        }

MyParentSerializer()

Result

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-174f3862f0ed> in <module>()
----> 1 MyParentSerializer()

/home/vagrant/.virtualenvs/housekeep/local/lib/python2.7/site-packages/rest_framework_serializer_extensions/serializers.pyc in __init__(self, *args, **kwargs)
    157         super(ExpandableFieldsMixin, self).__init__(*args, **kwargs)
    158         self.expandable_fields = self._standardise_expandable_definitions(
--> 159             self.get_expandable_field_definitions()
    160         )
    161

/home/vagrant/.virtualenvs/housekeep/local/lib/python2.7/site-packages/rest_framework_serializer_extensions/serializers.pyc in _standardise_expandable_definitions(self, expandable_fields)
    188         return {
    189             key: self._standardise_expandable_definition(definition)
--> 190             for key, definition in six.iteritems(expandable_fields)
    191         }
    192

/home/vagrant/.virtualenvs/housekeep/local/lib/python2.7/site-packages/rest_framework_serializer_extensions/serializers.pyc in <dictcomp>((key, definition))
    188         return {
    189             key: self._standardise_expandable_definition(definition)
--> 190             for key, definition in six.iteritems(expandable_fields)
    191         }
    192

/home/vagrant/.virtualenvs/housekeep/local/lib/python2.7/site-packages/rest_framework_serializer_extensions/serializers.pyc in _standardise_expandable_definition(self, definition)
    214         # The model used for the HashId (defaults to the serializer's model)
    215         if 'id_model' not in definition:
--> 216             definition['id_model'] = definition['serializer'].Meta.model
    217
    218         # read_only defaults to True

AttributeError: type object 'MyChildSerializer' has no attribute 'Meta'

There is a workaround: specify id_model in the expandable field definition with a value of None.

class MyParentSerializer(extension_serializers.ExpandableFieldsMixin, serializers.Serializer):
    class Meta:
        expandable_fields = {
            "child": {
                "serializer": MyChildSerializer,
                "id_model": None,
        }