django-rest-auth icon indicating copy to clipboard operation
django-rest-auth copied to clipboard

Custom model FieldDoesNotExist

Open gabrielfarah opened this issue 7 years ago • 3 comments

Hi all, thanks for the great lib. If someone uses a custom model that has, for example, email and password and no username and try to use the library, it will encounter a django.core.exceptions.FieldDoesNotExist: User has no field named 'username'. The error is generated by django-allauth not by django-rest-auth and can be easily solved by setting the following fields in settings.py.

# On settings.py 
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'

A better explanation of those settings can be found in https://django-allauth.readthedocs.io/en/latest/advanced.html

It would be nice to put this somewhere on the docs. I can set it up. What do you think?

Thanks.

gabrielfarah avatar Sep 09 '18 06:09 gabrielfarah

Thanks, very helpful ! Just wanted to point out that a full list of settings can be found here : https://django-allauth.readthedocs.io/en/latest/configuration.html

oessaid avatar Dec 06 '19 15:12 oessaid

After adding this contants to settings.py I am experiencing this issue, here is my traceback I am using AbstractUser model class Exception in thread django-main-thread: Traceback (most recent call last): File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/db/models/options.py", line 672, in get_field return self.fields_map[field_name] KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner self.run() File "/usr/local/lib/python3.9/threading.py", line 910, in run self._target(*self._args, **self._kwargs) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run self.check(display_num_errors=True) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/core/management/base.py", line 487, in check all_issues = checks.run_checks( File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/core/checks/registry.py", line 88, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/core/checks/urls.py", line 14, in check_url_config return check_resolver(resolver) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/core/checks/urls.py", line 24, in check_resolver return check_method() File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/urls/resolvers.py", line 480, in check for pattern in self.url_patterns: File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/utils/functional.py", line 49, in get res = instance.dict[self.name] = self.func(instance) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/urls/resolvers.py", line 696, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/utils/functional.py", line 49, in get res = instance.dict[self.name] = self.func(instance) File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/urls/resolvers.py", line 689, in urlconf_module return import_module(self.urlconf_name) File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 850, in exec_module File "", line 228, in _call_with_frames_removed File "/home/tomms/Work/article/config/urls.py", line 28, in path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')), File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/urls/conf.py", line 38, in include urlconf_module = import_module(urlconf_module) File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1030, in _gcd_import File "", line 1007, in _find_and_load File "", line 986, in _find_and_load_unlocked File "", line 680, in _load_unlocked File "", line 850, in exec_module File "", line 228, in _call_with_frames_removed File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/dj_rest_auth/registration/urls.py", line 4, in from .views import RegisterView, VerifyEmailView, ResendEmailVerificationView File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/dj_rest_auth/registration/views.py", line 24, in from dj_rest_auth.registration.serializers import ( File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/dj_rest_auth/registration/serializers.py", line 194, in class RegisterSerializer(serializers.Serializer): File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/dj_rest_auth/registration/serializers.py", line 196, in RegisterSerializer max_length=get_username_max_length(), File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/allauth/utils.py", line 72, in get_username_max_length max_length = User._meta.get_field(USER_MODEL_USERNAME_FIELD).max_length File "/home/tomms/.local/share/virtualenvs/article-uLOaw2Hi/lib/python3.9/site-packages/django/db/models/options.py", line 674, in get_field raise FieldDoesNotExist( django.core.exceptions.FieldDoesNotExist: User has no field named 'username'

ladypython247 avatar Mar 05 '22 04:03 ladypython247

Add ACCOUNT_USER_MODEL_USERNAME_FIELD = None to your settings.py

smyja avatar Oct 30 '22 04:10 smyja