-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
don't import authtoken model until needed #3785
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. |
@@ -180,7 +186,7 @@ def authenticate(self, request): | |||
|
|||
def authenticate_credentials(self, key): | |||
try: | |||
token = self.model.objects.select_related('user').get(key=key) | |||
token = self.get_model().objects.select_related('user').get(key=key) | |||
except self.model.DoesNotExist: |
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_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's
admin.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
.