Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
lovelydinosaur commentedJan 5, 2016
Possibly valid, yes - although I expect that there are already folks using the |
sheppard commentedJan 5, 2016
Current behavior should still work via this bit: defget_model(self):ifself.modelisnotNone:returnself.model |
xordoquy commentedJan 5, 2016
Seconding@sheppard, I don't see how the proposed change would affect the original behavior. |
rest_framework/authentication.py Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Still referencingself.model here.
There was a problem hiding this comment.
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 commentedJan 5, 2016
Noted. My mistake. :) |
sheppard commentedJan 5, 2016
Should it be |
lovelydinosaur commentedJan 5, 2016
|
sheppard commentedJan 5, 2016
Ok, I fixed the issue and added some more test cases. |
don't import authtoken model until needed
lovelydinosaur commentedJan 5, 2016
👍 |
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 on
rest_frameworkwith 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's
admin.site, wq.db'srest.routerinterface, and therest_pandasmodule (though the latter isn't strictly a Django "app"). Any code imported in the__init__.pyfor those modules needs to defer importing models until they are needed (c.f.django/django#2228).Currently,
rest_framework.authenticationimports theTokenmodel 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 theTokenmodel until it is needed, thus avoiding the import error. As a side effect, this also means thatrest_framework.authtoken.modelsis 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 whenauthtokenwas not inINSTALLED_APPS, whereas this issue will occur whether or notauthtokenis inINSTALLED_APPS.