Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USER_DETAILS_SERIALIZER setting not being detected #181

Closed
Luctia opened this issue Nov 29, 2020 · 4 comments · Fixed by #182
Closed

USER_DETAILS_SERIALIZER setting not being detected #181

Luctia opened this issue Nov 29, 2020 · 4 comments · Fixed by #182
Labels
bug Something isn't working

Comments

@Luctia
Copy link

Luctia commented Nov 29, 2020

I have a custom user model in my project and thus would also like a custom user serializer. To do this, I have added the following lines in my settings.py:

REST_AUTH_SERIALIZERS = {
    "USER_DETAILS_SERIALIZER": "apps.custom_users.serializers.CustomUserSerializer"
}

Upon running my project, I'm getting the following error:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 366, in execute
    self.check()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 392, in check
    all_issues = self._run_checks(
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 64, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 407, in check
    for pattern in self.url_patterns:
  File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_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 "/app/config/urls.py", line 32, in <module>
    path('dj-rest-auth/', include('dj_rest_auth.urls')),
  File "/usr/local/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  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/urls.py", line 1, in <module>
    from dj_rest_auth.views import (LoginView, LogoutView, PasswordChangeView,
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/views.py", line 16, in <module>
    from .app_settings import (JWTSerializer, JWTSerializerWithExpiration, LoginSerializer,
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/app_settings.py", line 1, in <module>
    from dj_rest_auth.serializers import JWTSerializer as DefaultJWTSerializer
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/serializers.py", line 145, in <module>
    class UserDetailsSerializer(serializers.ModelSerializer):
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/serializers.py", line 149, in UserDetailsSerializer
    class Meta:
  File "/usr/local/lib/python3.8/site-packages/dj_rest_auth/serializers.py", line 151, in Meta
    fields = ('pk', UserModel.USERNAME_FIELD, UserModel.EMAIL_FIELD, 'first_name', 'last_name')
AttributeError: type object 'CustomUser' has no attribute 'EMAIL_FIELD'

It looks like this error is coming from the dj_rest_auth.serializers.UserDetailsSerializer class, so it seems to me like it is completely ignoring my setting. Am I formatting the setting wrong or is there something else going on?

@jerinpetergeorge jerinpetergeorge added the bug Something isn't working label Nov 30, 2020
@jerinpetergeorge
Copy link
Contributor

@Luctia Could you add your custom user model? I am just wondering that there is no EMAIL_FIELD in your model class.

@Luctia
Copy link
Author

Luctia commented Nov 30, 2020

@jerinpetergeorge My user model indeed does not have an EMAIL_FIELD. In my system, a user has a person, which is a model that has the e-mail address (as well as the first and last name). A person does not always have a user, but a person that doesn't have a user sometimes needs to be mailed, so I can't move the e-mail field. As far as I can find, EMAIL_FIELD also can't be a relationship (e.g. EMAIL_FIELD = 'person.email'). Is this EMAIL_FIELD absolutely necessary in Django? I must admit I'm quite new to Django.

@Luctia
Copy link
Author

Luctia commented Nov 30, 2020

@jerinpetergeorge thanks for the PR and the explanation in the comments there. I'm just wondering, will this fix the entire issue? You've changed UserDetailsSerializer, but my original problem was that the REST_AUTH_SERIALIZERS.USER_DETAILS_SERIALIZER setting wasn't being detected, and I don't think the PR will do much to fix that.

@jerinpetergeorge
Copy link
Contributor

as you can see in the traceback, dj-rest-auth tried to import the default settings as well as the custom settings, but during the process, we have got an exception and the process got interrupted.

In short, the root cause has been fixed.

Brechard added a commit to Brechard/dj-rest-auth that referenced this issue Apr 26, 2021
Similar to the issue iMerica#181, dj-rest=auth was not loading my custom UserDetailsSerializer and when I use the default one there are two fields that my UserModel does not have. We should not enforce people to have this fields either and therefore I have done the same as the previous PR but with "first_name" and "last_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants