Python 属性错误:';CustomUser';对象没有属性';名字';

Python 属性错误:';CustomUser';对象没有属性';名字';,python,django,python-3.x,django-admin,customization,Python,Django,Python 3.x,Django Admin,Customization,这个错误意味着什么 AttributeError:“CustomUser”对象没有属性“first\u name” 自定义用户/models.py from time import timezone from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin from django.core.mail import s

这个错误意味着什么

AttributeError:“CustomUser”对象没有属性“first\u name”

自定义用户/models.py

from time import timezone
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.core.mail import send_mail
from django.utils.translation import ugettext_lazy as _
from datetime import datetime
now = datetime.now()

class CustomUserManager(BaseUserManager):
    def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """

        if not email:
            raise ValueError('The given email must be set')

        email = self.normalize_email(email)
        user = self.model(email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser, last_login=now,
                           **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        return self._create_user(email, password, False, False, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        return self._create_user(email, password, True, True, **extra_fields)

class CustomUser(AbstractBaseUser, PermissionsMixin):
    username    = models.CharField(max_length=254, unique=True)
    email       = models.EmailField(blank=True, unique=True)
    address1    = models.CharField(max_length=254, blank=True)
    address2    = models.CharField(max_length=254, blank=True)
    area_code   = models.CharField(max_length=20, blank=True)
    country_code     = models.CharField(max_length=10, blank=True)

    is_active    = models.BooleanField(default=True)
    is_admin     = models.BooleanField(default=False)
    is_staff     = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'address1', 'address2', 'area_code', 'country_code']

    objects = CustomUserManager()

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        """
        Returns the first_name plus the last_name, with a space in between.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        "Returns the short name for the user."
        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email], **kwargs)

我一直在尝试为普通用户和超级用户创建自定义用户注册表单,但我仍然无法以超级用户/管理员身份登录。我也不能注册任何用户,因为它没有连接到数据库。我按照本教程学习如何创建自定义用户,并设法做到这一点。我只是不能作为管理员登录,也不能注册任何用户。就我所见,您的自定义模型(
CustomUser
)中没有
first\u name
属性,下一步将做什么。这是什么

AttributeError:“CustomUser”对象没有属性“first\u name”

他在告诉你

查看您的
CustomUser
课程。它正在定义
用户名
电子邮件
等,但它没有在任何地方定义
名字。但是,您正试图在多个方法(即,
get\u full\u name()
get\u short\u name()
)中引用
first\u name
(未为类/模型定义)

尝试定义它并查看错误是否仍然存在