-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Possibly valid, yes - although I expect that there are already folks using the |
|
Current behavior should still work via this bit: def get_model(self):
if self.model is not None:
return self.model |
|
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 referencing self.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.
Noted. My mistake. :) |
|
Should it be |
|
|
|
Ok, I fixed the issue and added some more test cases. |
don't import authtoken model until needed
|
👍 |
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.