Proxy User and AUTH_USER_MODEL: TypeError: MyUser cannot proxy the swapped model 'myapp.MyUser'.

928 views
Skip to first unread message

Nicolas Pantel

unread,
Apr 14, 2018, 6:34:59 PM4/14/18
to Django users
Hi,

I need help on the topic: Proxy for User model referenced as AUTH_USER_MODEL.

As stated in the doc, needing to add some methods to User model, I tried to make a 'proxy' model.

I made a 'from scratch app', with only a MyUser(User) model, and AUTH_USER_MODEL = 'myapp.MyUser' in settings.py
MyUser has only 'class Meta: proxy = True'

On a ./manage.py shell (for example), I have the following traceback:

Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/home/npantel/proxy_user/proxy_user/models.py", line 4, in <module>
    class MyUser(User):
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 158, in __new__
    raise TypeError("%s cannot proxy the swapped model '%s'." % (name, base_meta.swapped))
TypeError: MyUser cannot proxy the swapped model 'proxy_user.MyUser'.

Anybody already tried to use proxy on User model?

Thanks,
Nicolas Pantel

Tim Graham

unread,
Apr 15, 2018, 9:10:58 PM4/15/18
to Django users
The behavior seems expected. https://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-users-and-proxy-models

It looks like if you use a proxy model, then you should import it where needed rather than use AUTH_USER_MODEL.

Eugene Kulak

unread,
Jun 22, 2020, 8:00:49 PM6/22/20
to Django users
It is not clear why it is not possible and what is the rationale behind this limitation? I see a lot of questions about this error. Perhaps a nice receipt what to do instead could help. For my understanding using the Proxy model as Custom User Model is most natural way to override some User's methods (like set password, etc)... Importing my custom model instead is not an option, as the User model used in Admin and standard views.

Thanks,
Reply all
Reply to author
Forward
0 new messages