Skip to content

Ensure _resolve_model does not return None #2086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2014

Conversation

beck
Copy link
Contributor

@beck beck commented Nov 18, 2014

Under certain circumstances Django may return None with get_model:
https://github.com/django/django/blob/1.6.7/django/db/models/loading.py#L265

It usually happens with circular imports so it is important that DRF excepts early, otherwise fault happens downstream and is much more difficult to debug.

The downstream failure looks something like:

Traceback (most recent call last):
  File "/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 102, in inner_run
    self.validate(display_num_errors=True)
  File "/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name, True)
  File "/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
...
[code with hidden circular import]
....
  File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 204, in __init__
    self.fields = self.get_fields()
  File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 240, in get_fields
    default_fields = self.get_default_fields()
  File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 722, in get_default_fields
    field = self.get_related_field(model_field, related_model, to_many)
  File "/lib/python2.7/site-packages/rest_framework/serializers.py", line 824, in get_related_field
    'queryset': related_model._default_manager,
AttributeError: 'NoneType' object has no attribute '_default_manager'

tomchristie added a commit that referenced this pull request Nov 28, 2014
Ensure _resolve_model does not return None
@tomchristie tomchristie merged commit 6fbd23a into encode:master Nov 28, 2014
@tomchristie
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants