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

Error when using custom user model #146

Open
JamesRamm opened this issue Nov 4, 2016 · 1 comment
Open

Error when using custom user model #146

JamesRamm opened this issue Nov 4, 2016 · 1 comment

Comments

@JamesRamm
Copy link

I get the following error on the documentation root page:
ImproperlyConfigured: Field name usernameis not valid for modelCustomUser.

I imagine this has something to do with my custom user model, where I have disabled the username field by setting USERNAME_FIELD = 'email' in the model. The rest API works fine and it is only when setting url(r'^api/v1/', include('rest_framework_docs.urls')), that I get the problem at /api/v1/

The traceback indicates it is coming from the following lines in api_endpoint.py:

fields = [{
                "name": key,
                "type": str(field.__class__.__name__),
                "required": field.required
            } for key, field in serializer().get_fields().items()]

However, I can enter the python shell, get an instance of the serializer and run this code manually just fine. So I'm now at a loss as to what is causing this...

The full traceback is:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py", line 184, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/views/generic/base.py", line 157, in get
    context = self.get_context_data(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/views.py", line 17, in get_context_data
    docs = ApiDocumentation()
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/api_docs.py", line 21, in __init__
    self.get_all_view_names(root_urlconf.urlpatterns)
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/api_docs.py", line 27, in get_all_view_names
    self.get_all_view_names(urlpatterns=pattern.url_patterns, parent_pattern=parent_pattern)
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/api_docs.py", line 29, in get_all_view_names
    api_endpoint = ApiEndpoint(pattern, parent_pattern)
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/api_endpoint.py", line 19, in __init__
    self.fields = self.__get_serializer_fields__()
  File "/usr/local/lib/python2.7/site-packages/rest_framework_docs/api_endpoint.py", line 54, in __get_serializer_fields__
    } for key, field in serializer().get_fields().items()]
  File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 1000, in get_fields
    field_name, info, model, depth
  File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 1135, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 1242, in build_unknown_field
    (field_name, model_class.__name__)
ImproperlyConfigured: Field name `username` is not valid for model `CustomUser`.

My serializer:

class CustomUserSerializer(serializers.ModelSerializer):
    client_profile = ClientProfileSerializer(required=False)
    instructor_profile = InstructorProfileSerializer(required=False)
    lookup_field = 'slug'

    class Meta:
        model = models.CustomUser
        fields = '__all__'

My model:

class OffPeaksUser(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
    '''Custom user model
    '''
    objects = UserManager()
    # Fields
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=200, unique=True, null=True)
    profile_image = models.ImageField(blank=True, null=True)
    title = models.CharField(max_length=10, blank=True, null=True)
    biography = models.TextField(blank=True, null=True)
    newsletter = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']
@whippersnapper
Copy link

*bump. Seeing something similar as well. Seems like when there are custom fields, I get the ImproperlyConfigured at /docs/ exception. Anybody have solution to this or point to how to configure proper?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants