微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用自定义用户进行django注册两步帐户时出现错误“为用户指定了未知字段student_id”

如何解决使用自定义用户进行django注册两步帐户时出现错误“为用户指定了未知字段student_id”

我在django-registration中使用两步帐户激活,运行服务器时出现此错误。我还使用了从AbstractBaseUser创建的自定义用户用户模型会通过student_id自动设置电子邮件字段。

forms.py

from django_registration.forms import RegistrationForm
from .models import StudentUser


class StudentUserRegisterForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = StudentUser

在models.py

class StudentUserManager(BaseUserManager):
def create_user(self,student_id,name,password):
    user = self.model(student_id=student_id,name=name,email=f'{student_id}@ksa.hs.kr')
    user.set_password(password)
    user.save()
    return user

def create_superuser(self,password):
    user = self.create_user(student_id,password)
    user.is_active = True
    user.is_superuser = True
    user.is_staff = True
    user.save()
    return user

class StudentUser(AbstractBaseUser,PermissionsMixin):
    object = StudentUserManager()
    student_id = models.CharField(max_length=10,verbose_name='학번',help_text='00-000',unique=True)
    name = models.CharField(max_length=100,verbose_name='이름')
    email = models.EmailField(default=f'{student_id}@ksa.hs.kr')
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_Now_add=True)
    USERNAME_FIELD = 'student_id'
    required_FIELDS = ['name']

urls.py中urlpatterns的一部分

path('accounts/register/',RegistrationView.as_view(form_class=StudentUserRegisterForm),name='django_registration_register'),path('accounts/',include('django_registration.backends.activation.urls')),

在setting.py

INSTALLED_APPS = [
'ksa_books_app','django.contrib.auth','django.contrib.admin','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django_registration',]
AUTH_USER_MODEL = 'ksa_books_app.StudentUser'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。