nuradilla
nuradilla

Reputation: 105

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

what this error means ?

AttributeError: 'CustomUser' object has no attribute '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)

i have been trying to create custom user registration form for normal user and super user but i still cant login as superuser/admin . also i cant register any user because it is not connected to db . i follow this tutorial on how to create custom user https://www.youtube.com/watch?v=5x97gGspzjY and i manage to do it . i just cant login as admin and cannot register any user . what exactly the next step

Upvotes: 1

Views: 4103

Answers (1)

cegas
cegas

Reputation: 3091

As far as I can see, you do not have first_name attribute in your custom model (CustomUser). This is what

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

is telling you.

Look at your CustomUser class. It is defining username, email, etc., but it does not define first_name anywhere. Yet, you are attempting to reference first_name (which is not defined for the class/model) in multiple methods (i.e., get_full_name(), get_short_name().

Try to define it and see whether the error persists.

Upvotes: 2

Related Questions