0

使用电子邮件作为主键实现自定义用户。安装 django rest 和 django-rest-auth 后,出现以下错误。

这是 django 抛出的错误:

django.core.exceptions.FieldDoesNotExist: BaseUser has no field named 'username'

django-rest-auth 是否有任何选项可以接受用户名作为电子邮件?我正在尝试覆盖序列化程序和视图,但我不确定这是否是最安全的选择?

模型.py

from django.db import models
from django.core.validators import RegexValidator
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser, PermissionsMixin
)

class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(email,
            password=password,
        )
        user.is_staff = True
        user.save(using=self._db)
        return user

# TODO: Password needs to be hashed
class BaseUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name='email',
        max_length=255,
        unique=True,
    )

    first_name = models.CharField(blank=True, max_length=30)
    last_name = models.CharField(blank=True, max_length=50)
    mobile_regex = RegexValidator(regex=r'^\+?\d{9,15}$', message="Please enter a max of 10 digits :)")
    mobile = models.CharField(validators=[mobile_regex], max_length=10, blank=True)
    is_a_student = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    def is_student(self):
        return self.is_a_student

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_admin(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_staff

设置.py

"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import dj_database_url
import logging
import copy
from django.utils.log import DEFAULT_LOGGING

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Application definition
AUTH_USER_MODEL = 'user.BaseUser'

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',


    # Third Party Apps
    'coverage',
    'herokuapp',
    'localflavor',
    'django_extensions',
    'django_nose',
    'rest_framework',
    # 'rest_framework_swagger',
    'rest_framework_docs',



    # Third Party Authentication
    'rest_framework.authtoken',
    'rest_auth',
    'allauth',
    'allauth.account',
    'rest_auth.registration',

    # Local Apps
    'user',
    'student',
    'school',
    'employer',
    'job'
)

#Rest_Framework admin config
#Only uncomment if API is viewable for admins
"""
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
    'PAGE_SIZE': 10
}
"""
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'



MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'student_job.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

INTERNAL_IPS = '127.0.0.1'
WSGI_APPLICATION = 'student_job.wsgi.application'
DJANGO_WYSIWYG_FLAVOR = "ckeditor"

使用 rest_swagger 进行回溯

(univjobs-back) mariacarmenjavelona@Marias-MacBook-Pro ~/Desktop/univjobs-back/student_job (feature/react_redux●)$ ./manage.py runserver_plus
/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/management/base.py:265: RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead
  RemovedInDjango110Warning)

 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
 * Restarting with stat
/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/management/base.py:265: RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead
  RemovedInDjango110Warning)

Performing system checks...

System check identified no issues (0 silenced).

Django version 1.9.4, using settings 'student_job.settings'
Development server is running at http://127.0.0.1:8000/
Using the Werkzeug debugger (http://werkzeug.pocoo.org/)
Quit the server with CONTROL-C.
 * Debugger is active!
 * Debugger pin code: 215-984-059
 * Debugger is active!
 * Debugger pin code: 201-308-245
/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/template/loader.py:97: RemovedInDjango110Warning: render() must be called with a dict, not a RequestContext.
  return template.render(context, request)

127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/ HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/css/highlight.default.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/css/atelier-dune.light.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/css/rest_framework_swagger.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/css/screen.css HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/images/wordnik_api.png HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/shred.bundle.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/jquery-1.8.0.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/jquery.slideto.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/jquery.wiggle.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/jquery.ba-bbq.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/jquery.cookie.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/handlebars-1.0.0.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/underscore-min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/swagger.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/swagger-ui.min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/highlight.8.0.pack.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /static/rest_framework_swagger/lib/backbone-min.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/ HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/ HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/login HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/logout HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/password HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/register HTTP/1.1" 200 -
Internal Server Error: /docs/api-docs/rest_auth/user
Traceback (most recent call last):
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/views.py", line 163, in get
    'apis': generator.generate(apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 52, in generate
    'operations': self.get_operations(api, apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 106, in get_operations
    inspector=method_introspector)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 1137, in discover_parameters
    method_params = inspector.get_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 309, in get_parameters
    form_params = self.build_form_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 433, in build_form_parameters
    fields = serializer().get_fields()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 939, in get_fields
    field_name, info, model, depth
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1076, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1182, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `username` is not valid for model `BaseUser`.
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/user HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/dj_static.py", line 83, in __call__
    return self.application(environ, start_response)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/six.py", line 659, in reraise
    raise value
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/views.py", line 163, in get
    'apis': generator.generate(apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 52, in generate
    'operations': self.get_operations(api, apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 106, in get_operations
    inspector=method_introspector)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 1137, in discover_parameters
    method_params = inspector.get_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 309, in get_parameters
    form_params = self.build_form_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 433, in build_form_parameters
    fields = serializer().get_fields()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 939, in get_fields
    field_name, info, model, depth
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1076, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1182, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `username` is not valid for model `BaseUser`.
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/login HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/logout HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/password HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/register HTTP/1.1" 200 -
Internal Server Error: /docs/api-docs/rest_auth/user
Traceback (most recent call last):
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/views.py", line 163, in get
    'apis': generator.generate(apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 52, in generate
    'operations': self.get_operations(api, apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 106, in get_operations
    inspector=method_introspector)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 1137, in discover_parameters
    method_params = inspector.get_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 309, in get_parameters
    form_params = self.build_form_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 433, in build_form_parameters
    fields = serializer().get_fields()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 939, in get_fields
    field_name, info, model, depth
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1076, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1182, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `username` is not valid for model `BaseUser`.
127.0.0.1 - - [09/Mar/2016 20:34:05] "GET /docs/api-docs/rest_auth/user HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/dj_static.py", line 83, in __call__
    return self.application(environ, start_response)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/six.py", line 659, in reraise
    raise value
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/views.py", line 163, in get
    'apis': generator.generate(apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 52, in generate
    'operations': self.get_operations(api, apis),
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/docgenerator.py", line 106, in get_operations
    inspector=method_introspector)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 1137, in discover_parameters
    method_params = inspector.get_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 309, in get_parameters
    form_params = self.build_form_parameters()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework_swagger/introspectors.py", line 433, in build_form_parameters
    fields = serializer().get_fields()
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 939, in get_fields
    field_name, info, model, depth
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1076, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/Users/mariacarmenjavelona/.virtualenvs/univjobs-back/lib/python3.5/site-packages/rest_framework/serializers.py", line 1182, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `username` is not valid for model `BaseUser`.
4

4 回答 4

2

当我有一个带有emailandpassword和 no的自定义用户模型时,这发生在我身上username。为了解决这个问题,我在我的settings.py:

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

问题不是由 产生的,django-rest-auth而是由产生的django-allauth。可以在https://django-allauth.readthedocs.io/en/latest/advanced.htmlCustom User Models部分找到对这些设置的更好解释

于 2018-09-09T06:26:18.503 回答
1

如果您使用 0.6.0 版,您将遇到麻烦。请将项目环境更新为 Django-Rest-Auth 的 0.7.0 版本,并在您的自定义用户模型中设置 ACCOUNT_USER_MODEL_USERNAME_FIELD 参数。

如果你仍然有那个麻烦,让我知道如何重复这个麻烦?

于 2016-03-08T02:13:19.940 回答
0

如果您只需要登录和注销,您可以从 rest_auth.views 中删除 UserDetailsView 。django-rest-auth 文档告诉您像这样导入所有 url

urlpatterns = [
    ...,
    url(r'^rest-auth/', include('rest_auth.urls')),
]

但是你也可以只导入你需要的视图:

from rest_auth.views import (
    LoginView, LogoutView,
)

urlpatterns = [
    ...
    # REST Auth
    url(r'^api/v1/rest-auth/login/$', LoginView.as_view(), name='rest_login'),
    url(r'^api/v1/rest-auth/logout/$', LogoutView.as_view(), name='rest_logout'),
]

为我工作。希望能帮助到你。

于 2018-04-18T12:10:57.877 回答
0

您的 django 版本不支持您的 auth 包。

请使用 django 1.9.4 版本(最新版本)升级你的 django。

于 2016-03-08T13:22:12.930 回答