2017-10-05 105 views
1

這個錯誤的含義是什麼?AttributeError:'CustomUser'對象沒有屬性'first_name'

AttributeError的:「CustomUser」對象有沒有屬性「FIRST_NAME」

custom_user/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) 

我一直在試圖創建普通用戶和超級用戶,但我自定義用戶登記表仍然無法以超級用戶/管理員身份登錄。我也無法註冊任何用戶,因爲它沒有連接到數據庫。我按照這個教程如何創建自定義用戶https://www.youtube.com/watch?v=5x97gGspzjY,我設法做到這一點。我只是不能以管理員身份登錄,無法註冊任何用戶。到底什麼是下一步

回答

1

據我所見,您的自定義模型(CustomUser)中沒有first_name屬性。這是什麼

AttributeError: 'CustomUser' object has no attribute 'first_name'

告訴你。

看看你的CustomUser課程。它定義了username,email等,但它沒有在任何地方定義first_name。然而,你正在試圖引用first_name在多個方法(即get_full_name()get_short_name()

嘗試定義它,看看錯誤是否仍然存在(這是不是爲類/模型中定義)。