Django-rest-framework: separating serializers to different files causes an error

Created on 8 Oct 2015  路  1Comment  路  Source: encode/django-rest-framework

This one is a doozy, if all of the serializers defined in the same file, it would work, however, separating them will throw an error.

This would work:

class AuthorSerializer(serializers.ModelSerializer):
    class Meta:
        model = User


class AppSerializer(serializers.ModelSerializer):
    author = AuthorSerializer()
    class Meta:
        model = App

however, if I separate AuthorSerializer to a different file, and include it for AppSerializer, it would throw the following error:

Environment:


Request Method: GET
Request URL: http://localhost:8000/api/web/v1/search_apps?q=example&limit=10

Django Version: 1.8.4
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'django.contrib.sites',
 'my_project',
 'django_rq',
 'social.apps.django_app.default',
 'django_nose')
Installed Middleware:
('django.middleware.security.SecurityMiddleware',
 'django.middleware.cache.UpdateCacheMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'my_project.common.auth.MySocialAuthExceptionMiddleware')


Traceback:
File "/Users/me/Projects/env3/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/me/Projects/my_project/my_project/api/api.py" in web_loader
46.         return cls.loader(request, 'web', version, action)
File "/Users/me/Projects/my_project/my_project/api/api.py" in loader
40.             return func(request)
File "/Users/me/Projects/my_project/my_project/common/decorators.py" in decorated
51.         return view_func(*args, **kwargs)
File "/Users/me/Projects/my_project/my_project/common/decorators.py" in decorated
24.         print('_DICT_', JSONRenderer().render(data.data))
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in data
622.         ret = super(ListSerializer, self).data
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in data
217.                 self._data = self.to_representation(self.instance)
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
572.             self.child.to_representation(item) for item in iterable
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in <listcomp>
572.             self.child.to_representation(item) for item in iterable
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
439.                 ret[field.field_name] = field.to_representation(attribute)
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
426.         fields = [field for field in self.fields.values() if not field.write_only]
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in fields
317.             for key, value in self.get_fields().items():
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/serializers.py" in get_fields
847.         info = model_meta.get_field_info(model)
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/utils/model_meta.py" in get_field_info
67.     forward_relations = _get_forward_relationships(opts)
File "/Users/me/Projects/env3/lib/python3.4/site-packages/rest_framework/utils/model_meta.py" in _get_forward_relationships
113.                 not field.rel.through._meta.auto_created

Exception Type: AttributeError at /api/web/v1/search_apps
Exception Value: 'NoneType' object has no attribute '_meta'

Most helpful comment

ugh ignore this, I made a stupid mistake of including a different User model

>All comments

ugh ignore this, I made a stupid mistake of including a different User model

Was this page helpful?
0 / 5 - 0 ratings