def _standardise_expandable_definition(self, definition): """ Return a consistent field definition dictionary. """ if not isinstance(definition, dict): definition = dict(serializer=definition) # Resolve string references to serializers if isinstance(definition['serializer'], six.string_types): reference = definition['serializer'] serializer = utils.import_local(reference) assert issubclass(serializer, serializers.BaseSerializer), ( "{0} is not a serializer".format(reference)) definition['serializer'] = serializer # Custom expansion uses no other fields if definition['serializer'] == serializers.SerializerMethodField: definition['id_source'] = False return definition # The model used for the HashId (defaults to the serializer's model) 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 if 'read_only' not in definition: definition['read_only'] = True return definition
def test_import_complex_path_within_installed_apps(self): imported = utils.import_local( 'test_package.test_module.serializers.TestSerializer') self.assertIs(imported, TestSerializer)
def test_import_within_installed_apps(self): imported = utils.import_local( 'rest_framework_serializer_extensions.fields.HashIdField') self.assertIs(imported, fields.HashIdField)
def test_fail_missing_module(self): with self.assertRaises(ImportError): utils.import_local('tests.not_found.not_found')
def test_fail_missing_attribute(self): with self.assertRaises(AttributeError): utils.import_local('tests.not_found')
def test_fails_if_not_in_apps(self): with self.assertRaises(AssertionError): utils.import_local('collections.defaultdict')