我使用了Django rest框架和自定义用户模型。
[urls.py]
cellForRow
[model.py]
from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import refresh_jwt_token
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('rest_auth.urls')),
path('registration/', include('rest_auth.registration.urls')),
path('refresh-token/', refresh_jwt_token),
]
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser, PermissionsMixin
)
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError(_('Users must have an email address'))
user = self.model(
email=self.normalize_email(email),
username=username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email=email,
password=password,
username=username,
)
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name=_('Email address'),
max_length=255,
unique=True,
)
username = models.CharField(
verbose_name=_('username'),
max_length=30,
unique=True
)
is_active = models.BooleanField(
verbose_name=_('Is active'),
default=True
)
date_joined = models.DateTimeField(
verbose_name=_('Date joined'),
default=timezone.now
)
salt = models.CharField(
verbose_name=_('Salt'),
max_length=10,
blank=True
)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', ]
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
ordering = ('-date_joined',)
def __str__(self):
return self.username
def get_full_name(self):
return self.username
def get_short_name(self):
return self.username
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All superusers are staff
return self.is_superuser
get_full_name.short_description = _('Full name')
类的save_user(),具有DefaultAccountAdapter
和first_name
。
因此,我继承了last_name
并删除了first_name和last_name这样。 (注释是原始来源和我的新adapter.py之间的区别)
[adapter.py]
DefaultAccountAdapter
我还为settings.py定义了from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.utils import user_username, user_email, user_field
class UserAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
data = form.cleaned_data
#first_name = data.get('first_name')
#last_name = data.get('last_name')
email = data.get('email')
username = data.get('username')
user_email(user, email)
user_username(user, username)
# if first_name:
# user_field(user, 'first_name', first_name)
# if last_name:
# user_field(user, 'last_name', last_name)
if 'password1' in data:
user.set_password(data["password1"])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
user.save()
return user
。
这样做之后,当我请求注册用户(/ registration)时,它将引发错误。
ACCOUNT_ADAPTER = 'users.adapter.UserAdapter'
我已经删除了first_name和last_name,但是它抛出的first_name是无效的。
有什么解决办法吗?
谢谢!