Django-rest-framework: TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.

Created on 29 Sep 2020  路  7Comments  路  Source: encode/django-rest-framework

Duplicate of #7442

Checklist

  • [x] I have verified that that issue exists against the master branch of Django REST framework.
  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [x] I have reduced the issue to the simplest possible case.
  • [x] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

Run python manage.py runserver or any command involving building Django (Mypy in my case).

PR: https://github.com/aims-group/metagrid/pull/142

Expected behavior

Django should build without issues.

Actual behavior

Upgrading from 3.11.1 to 3.12.x causes build failure.

Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.8/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 89, in main
  File "mypy/build.py", line 180, in build
  File "mypy/build.py", line 227, in _build
  File "mypy/build.py", line 470, in load_plugins
  File "mypy/build.py", line 448, in load_plugins_from_config
  File "/usr/local/lib/python3.8/site-packages/mypy_django_plugin/main.py", line 80, in __init__
    self.django_context = DjangoContext(django_settings_module)
  File "/usr/local/lib/python3.8/site-packages/mypy_django_plugin/django/context.py", line 88, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "/usr/local/lib/python3.8/site-packages/mypy_django_plugin/django/context.py", line 72, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/models.py", line 4, in <module>
    TokenModel = import_string(getattr(settings, 'REST_AUTH_TOKEN_MODEL', 'rest_framework.authtoken.models.Token'))
  File "/usr/local/lib/python3.8/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/site-packages/rest_framework/authtoken/models.py", line 43, in <module>
    class TokenProxy(Token):
  File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 177, in __new__
    raise TypeError(
TypeError: Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.

Most helpful comment

EDITED: I had this error when I have 'rest_framework' in my INSTALLED APPS . I solved it by adding in settings.py

INSTALLED_APPS = [
...
   'rest_framework',
...
    'rest_framework.authtoken', #this needs to be added
...
]

All 7 comments

Token is only abstract if rest_framework.authtoken is not installed, so it looks like we'd need to adjust TokenProxy (to be abstract too or to block it out entirely) in that case. 馃

EDITED: I had this error when I have 'rest_framework' in my INSTALLED APPS . I solved it by adding in settings.py

INSTALLED_APPS = [
...
   'rest_framework',
...
    'rest_framework.authtoken', #this needs to be added
...
]

I had this error when I try to flush db also. I solved it by adding in settings.py

INSTALLED_APPS = [
...
    'rest_framework.authtoken',
...
]

In my case, I am not using TokenAuthentication. Instead, I am using django-rest-framework-simplejwt with dj-rest-auth, so adding rest_framework.authtoken would be a monkey patch that doesn't necessarily deal with the root cause.

In my case, I am not using TokenAuthentication. Instead, I am using django-rest-framework-simplejwt with dj-rest-auth, so adding rest_framework.authtoken would be a monkey patch that doesn't necessarily deal with the root cause.

My bad. I forgot to say that I have 'rest_framework', in my settings.py also. To be clear this is an error when using rest_framework not when I am flushing my db.

That is duplicate of https://github.com/encode/django-rest-framework/issues/7442, that change was released in 3.12.

Yes, thanks @kalekseev 鈥斅營 had in my head that this had been resolved (but it's just at the issue stage, not PR.)

Let's close this here and focus on the original. Solutions are in the comments there, if anyone wants to make a PR.

Duplicate of #7442

Me too

INSTALLED_APPS = [
'django.contrib.admin',
'django_extensions',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'django_filters',

]

Was this page helpful?
0 / 5 - 0 ratings