为什么名字和姓氏字段没有输入数据库,而只是我创建的自定义用户中的电子邮件和密码?

Why firstname and lastname fields are not entering in database but just the email and password in the customUser i created?

我从 AbstractBaseUser 创建了一个自定义用户,其中包含内置的电子邮件、名字、姓氏和密码字段,以及来自 BaseUserManager 的 userManager,如下所示。但是只有电子邮件和密码被提交到数据库中,而不是姓氏和名字。我使用 mysql 作为 dbms。

型号

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# Create your models here.  

class UserManager(BaseUserManager):
    def create_user(self, email, firstname, lastname, password=None, is_active=True, is_staff=False, is_admin=False):
        if not email:
            raise ValueError("user must have an email")
        user_obj = self.model(
            email = self.normalize_email(email)
        )
        user_obj.set_password(password)
        user_obj.active = is_active
        user_obj.staff = is_staff
        user_obj.admin = is_admin
        user_obj.save(using=self._db)
        return user_obj

    def create_staffuser(self, email, firstname, lastname, password=None):
        user = self.create_user(email, firstname, lastname, password=password, is_staff=True)
        return user

    def create_superuser(self, email, firstname, lastname, password=None):
        user = self.create_user(email, firstname, lastname, password=password, is_staff=True, is_admin=True)
        return user


class customUser(AbstractBaseUser):
    email       = models.EmailField(max_length=225, unique=True)
    firstname   = models.CharField(max_length=225)
    lastname    = models.CharField(max_length=225)
    active      = models.BooleanField(default=True)
    staff       = models.BooleanField(default=False)
    admin       = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin

    @property
    def is_active(self):
        return self.active

我传递了我的注册模板中的值

{% extends 'base1.html' %}
{% block content %}
<div class="container">
<form action="signup" method="POST">
{% csrf_token %}
<label for=""> First Name : </label>
<input type="text" name="firstname" placeholder="Enter Firstname"><br><br>
<label for="">Last Name : </label>
<input type="text" name="lastname" placeholder="Enter Lastname"><br><br>
<label for=""> E-mail : </label>
<input type="text" name="email" placeholder="Enter Email"><br><br>
<label for=""> Password : </label>
<input type="password" name="password1"><br><br>
<label for=""> Confirm Password : </label>
<input type="password" name="password2"><br><br>
<input type="submit" value="Signup" class="btn btn-primary">
</form>

<div>
{% for message in messages %}
<h3> {{message}} </h3>
{% endfor %}

</div>

</div>
{% endblock %}

并使用views.py将其连接到模型

def signup(request):
    if request.method == "POST":
        firstname1 = request.POST['firstname']
        lastname1 = request.POST['lastname']
        email1 = request.POST['email']
        password1 = request.POST['password1']
        password2 = request.POST['password2']
        if password1 == password2:
            if customUser.objects.filter(email=email1).exists():
                messages.info(request,'email already taken')
                return redirect('signup')
            else:
                user = customUser.objects.create_user(firstname=firstname1, lastname=lastname1, email=email1, password=password1)
                user.save()
                return redirect('/')
        else:
            messages.info(request, 'Password Doesnt Match')
            return redirect('signup')
    else:   
        return render(request, 'signup.html')

但我的数据库中只有电子邮件、密码正在存储,每当新用户注册时,名字和姓氏字段都是空的。 我提供 urls.py 以防万一

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views
app_name = 'login'

urlpatterns = [
    path('ktupage/', views.ktupage, name='ktupage'),
    path('mgcourses/', views.mgcourses, name='mgcourses'),
    path('calicutcourses/', views.calicutcourses, name='calicutcourses'),
    path('mgcourses/mgaas/', views.mgaas, name='mgaas'),
    path('mgcourses/mgpara/', views.mgpara, name='mgpara'),
    path('calicutcourses/calicutaas/', views.calicutaas, name='calicutaas'),
    path('calicutcourses/calicutpara/', views.calicutpara, name='calicutpara'),
    path('login/', views.login, name='login'),
    path('logout/', auth_views.LogoutView.as_view(),name='logout'),
    path('signup', views.signup, name='signup'),
]

你只是忘了在 create_user 方法中设置它们:

    def create_user(self, email, firstname, lastname, password=None, is_active=True, is_staff=False, is_admin=False):
        if not email:
            raise ValueError("user must have an email")
        user_obj = self.model(
            email = self.normalize_email(email)
        )
        user_obj.set_password(password)
        user_obj.active = is_active
        user_obj.staff = is_staff
        user_obj.admin = is_admin
        user_obj.firstname = firstname  # <------
        user_obj.lastname = lastname    # <------
        user_obj.save(using=self._db)
        return user_obj