Fix: authtoken.TokenProxy cannot be proxy when not installed (#7571)

closes https://github.com/encode/django-rest-framework/issues/7442
This commit is contained in:
Kryštof Pilnáček 2020-10-09 13:16:15 +02:00 committed by GitHub
parent 931b34e7de
commit 79daf315c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -49,5 +49,6 @@ class TokenProxy(Token):
return self.user.pk return self.user.pk
class Meta: class Meta:
proxy = True proxy = 'rest_framework.authtoken' in settings.INSTALLED_APPS
abstract = 'rest_framework.authtoken' not in settings.INSTALLED_APPS
verbose_name = "token" verbose_name = "token"

View File

@ -1,10 +1,11 @@
import importlib
from io import StringIO from io import StringIO
import pytest import pytest
from django.contrib.admin import site from django.contrib.admin import site
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management import CommandError, call_command from django.core.management import CommandError, call_command
from django.test import TestCase from django.test import TestCase, modify_settings
from rest_framework.authtoken.admin import TokenAdmin from rest_framework.authtoken.admin import TokenAdmin
from rest_framework.authtoken.management.commands.drf_create_token import \ from rest_framework.authtoken.management.commands.drf_create_token import \
@ -21,6 +22,14 @@ class AuthTokenTests(TestCase):
self.user = User.objects.create_user(username='test_user') self.user = User.objects.create_user(username='test_user')
self.token = Token.objects.create(key='test token', user=self.user) self.token = Token.objects.create(key='test token', user=self.user)
def test_authtoken_can_be_imported_when_not_included_in_installed_apps(self):
import rest_framework.authtoken.models
with modify_settings(INSTALLED_APPS={'remove': 'rest_framework.authtoken'}):
importlib.reload(rest_framework.authtoken.models)
# Set the proxy and abstract properties back to the version,
# where authtoken is among INSTALLED_APPS.
importlib.reload(rest_framework.authtoken.models)
def test_model_admin_displayed_fields(self): def test_model_admin_displayed_fields(self):
mock_request = object() mock_request = object()
token_admin = TokenAdmin(self.token, self.site) token_admin = TokenAdmin(self.token, self.site)