使用自定义用户进行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 (将#修改为@)