Sahand
Sahand

Reputation: 8360

Proxy model error: Local field in class clashes with field of the same name from base class

class EmailUser(User):

    class Meta:
        proxy = True

    email = models.EmailField(_('email address'), unique=True)

In the above, I'm trying to implement a proxy model, but I'm getting this error message:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10e04ed08>
Traceback (most recent call last):
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/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 "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/workout/workoutcal/models.py", line 28, in <module>
    class EmailUser(User):
  File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/base.py", line 231, in __new__
    base.__name__,
django.core.exceptions.FieldError: Local field 'username' in class 'EmailUser' clashes with field of the same name from base class 'User'.

I wonder why I get this error, and what to do about it. I'm using Django version 1.11.

Upvotes: 0

Views: 761

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You can't do what you're trying to do with a proxy model. Luckily, Django makes it easy to extend the built-in User model: create a normal concrete model that inherits from AbstractUser, and set the AUTH_USER_MODEL setting to the name of your model.

Upvotes: 1

Related Questions