Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

don't import authtoken model until needed#3785

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lovelydinosaur merged 4 commits intoencode:masterfromsheppard:authtoken-import
Jan 5, 2016

Conversation

@sheppard
Copy link
Contributor

This PR updates the usage of the authtoken model for better compatibility with Django 1.9, specifically to make it possible to define module-level app APIs that rely onrest_framework with the default authentication settings (which importrest_framework.authentication).

Some background: it is forbidden in Django 1.9 to import models before the app infrastructure has finished loading. This makes it a bit tricky when implementing module-level shortcut APIs like Django'sadmin.site, wq.db'srest.router interface, and therest_pandas module (though the latter isn't strictly a Django "app"). Any code imported in the__init__.py for those modules needs to defer importing models until they are needed (c.f.django/django#2228).

Currently,rest_framework.authentication imports theToken model at the top level and therefore cannot be imported in Django 1.9 until after apps are done initializing (see the traceback below). This patch simply defers loading theToken model until it is needed, thus avoiding the import error. As a side effect, this also means thatrest_framework.authtoken.models is never imported unless it is being used, which means the workaround for#705 is no longer needed. This issue is similar but not the same - in#705, the error occured whenauthtoken was not inINSTALLED_APPS, whereas this issue will occur whether or notauthtoken is inINSTALLED_APPS.

  (test initialization stuff...)  File"/local/path/wq.db/tests/__init__.py", line10, in<module>    django.setup()  File"/usr/local/lib/python3.4/dist-packages/django/__init__.py", line18, insetup    apps.populate(settings.INSTALLED_APPS)  File"/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line85, inpopulate    app_config= AppConfig.create(entry)  File"/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line90, increate    module= import_module(entry)  File"/usr/lib/python3.4/importlib/__init__.py", line109, inimport_modulereturn _bootstrap._gcd_import(name[level:], package, level)  File"/local/path/wq.db/wq/db/rest/__init__.py", line2, in<module>from .routersimport ModelRouter, router# NOQA  File"/local/path/wq.db/wq/db/rest/routers.py", line7, in<module>from rest_framework.routersimport DefaultRouter, Route  File"/usr/local/lib/python3.4/dist-packages/rest_framework/routers.py", line25, in<module>from rest_frameworkimport views  File"/usr/local/lib/python3.4/dist-packages/rest_framework/views.py", line98, in<module>classAPIView(View):  File"/usr/local/lib/python3.4/dist-packages/rest_framework/views.py", line103, inAPIView    authentication_classes= api_settings.DEFAULT_AUTHENTICATION_CLASSES  File"/usr/local/lib/python3.4/dist-packages/rest_framework/settings.py", line203, in__getattr__    val= perform_import(val, attr)  File"/usr/local/lib/python3.4/dist-packages/rest_framework/settings.py", line148, inperform_importreturn [import_from_string(item, setting_name)for itemin val]  File"/usr/local/lib/python3.4/dist-packages/rest_framework/settings.py", line148, in<listcomp>return [import_from_string(item, setting_name)for itemin val]  File"/usr/local/lib/python3.4/dist-packages/rest_framework/settings.py", line160, inimport_from_string    module= importlib.import_module(module_path)  File"/usr/lib/python3.4/importlib/__init__.py", line109, inimport_modulereturn _bootstrap._gcd_import(name[level:], package, level)  File"/usr/local/lib/python3.4/dist-packages/rest_framework/authentication.py", line13, in<module>from rest_framework.authtoken.modelsimport Token  File"/usr/local/lib/python3.4/dist-packages/rest_framework/authtoken/models.py", line16, in<module>classToken(models.Model):  File"/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line94, in__new__    app_config= apps.get_containing_app_config(module)  File"/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line239, inget_containing_app_configself.check_apps_ready()  File"/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line124, incheck_apps_readyraise AppRegistryNotReady("Apps aren't loaded yet.")django.core.exceptions.AppRegistryNotReady:Apps aren't loaded yet.

@lovelydinosaur
Copy link
Contributor

Possibly valid, yes - although I expect that there are already folks using themodel = attribute, so we might want to be more careful about preserving any current behavior.

@sheppard
Copy link
ContributorAuthor

Current behavior should still work via this bit:

defget_model(self):ifself.modelisnotNone:returnself.model

@xordoquy
Copy link
Contributor

Seconding@sheppard, I don't see how the proposed change would affect the original behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Still referencingself.model here.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Oops - I suppose some tests would help.

@lovelydinosaur
Copy link
Contributor

Current behavior should still work via this bit

Noted. My mistake. :)

@sheppard
Copy link
ContributorAuthor

Should it beget_model() or_get_model()? I don't this needs to be a public API.

@lovelydinosaur
Copy link
Contributor

get_model() is fine - we're not documenting it, which is what we consider our baseline for "is this public, and subject to the deprecation policy?'

@sheppard
Copy link
ContributorAuthor

Ok, I fixed the issue and added some more test cases.

lovelydinosaur added a commit that referenced this pull requestJan 5, 2016
don't import authtoken model until needed
@lovelydinosaurlovelydinosaur merged commit37f7b76 intoencode:masterJan 5, 2016
@lovelydinosaur
Copy link
Contributor

👍

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

Projects

None yet

Milestone

3.3.3 Release

Development

Successfully merging this pull request may close these issues.

3 participants

@sheppard@lovelydinosaur@xordoquy

[8]ページ先頭

©2009-2025 Movatter.jp