9

I am building a custom User class in django to use in creating a signup application and I keep on getting the error above every time I try to makemigrations. As far as I can see, my code is per django documentation here.. I also have AUTH_USER_MODEL correctly placed in my settings configurations. Here's my models.py

`class MyUserManager(BaseUserManager): def create_user(self, email, first_name,last_name,profile_picture,phone_no,password=None): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('Users must have an email address') user = self.model( email=self.normalize_email(email), first_name=first_name, last_name=last_name, profile_picture=profile_picture, phone_no=phone_no, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password): """ Creates and saves a superuser with the given email and password. """ SuperUser = self.create_user( email, password=password, ) SuperUser.staff = True SuperUser.admin = True SuperUser.save(using=self._db) return SuperUser class MyUser(AbstractBaseUser): email = models.EmailField( verbose_name = 'email_address', max_length=255, unique=True, # validators=email_validator, ) first_name = models.CharField(max_length=20,blank=False,null=False) last_name = models.CharField(max_length=20,blank=False,null=False) phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+254 ...'") phone_no = models.CharField(validators=[phone_regex], max_length=17, blank=False) profile_picture = models.ImageField(upload_to='media/',blank=False) # email_validator = EmailValidator(message='Invalid email # address',code=None,whitelist=None) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) objects = MyUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['first_name','last_name','phone_no','profile_picture'] # Email & Password are required by default def get_full_name(self): return self.email def get_short_name(): return self.email def __str__(self): return self.email def has_perm(self,perm,obj=None): #does user have a specific permission return True def has_module_pers(self,app_label): #does user have permissions to view the app 'app_label' return True @property def is_admin(self): return self.is_admin @property def is_active(self): return self.is_active # hook in the New Manager to our Model class MyUser(AbstractBaseUser): ... objects = MyUserManager() ` 
5
  • Is your multi-line comment not having multiple #s a copy-paste error or is that what you're running? You should fix it, it makes your whole script look like a string Commented Jul 12, 2018 at 14:43
  • Your class donot have user_name field attribute in it so far from this code.... Commented Jul 12, 2018 at 14:43
  • @MoxieBall that's a copy-paste error, in my editor the comment is a single line Commented Jul 12, 2018 at 15:08
  • You should fix the code in your question Commented Jul 12, 2018 at 15:11
  • @Marcus.Aurelianus is it really a must i have the field user_name? Coz I haven't seen it in the documentation. Commented Jul 12, 2018 at 15:14

5 Answers 5

24

TO create custom User Model

class User(AbstractUser): """User model.""" username = None email = models.EmailField(_('email address'), unique=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] 

Official Documentation for CustomUser

You are: - Extending the base class that Django has for User models.

  • Removing the username field.
  • Making the email field required and unique.
  • List itemTelling Django that you are going to use the email field as the USERNAME_FIELD
  • Removing the email field from the REQUIRED_FIELDS settings (it is automatically included as USERNAME_FIELD)

Source Link

Sign up to request clarification or add additional context in comments.

Comments

3

USERNAME_FIELD must be UNIQUE when you create custom User model

So you can just make unique Email field like Roshan says, but some cases Email can not be unique for some services.

So I prefer just make one uuid field and make it USERNAME_FIELD like this

class User(AbstractBaseUser): uid = models.UUIDField( default=None, blank=True, null=True, unique=True, ) USERNAME_FIELD = "uid" 

Comments

0

Your code has a multiline comment that is ill-formatted

#email_validator = EmailValidator(message='Invalid email address',code=None,whitelist=None)

the second line of that comment is not actually commented so the single quote after address is opening a string that appears to contain the rest of the class and is closed at the stray single quote at the very end.

1 Comment

that's a copy-paste error, in my editor it's well formatted.
0

If it's not working after adding the field also,

check if the value passed is in string format :

Wrong -> USERNAME_FIELD = email

Correct -> USERNAME_FIELD = 'email'

Comments

0
from django.db import models from django.contrib.auth.models import AbstractUser class usertype(models.Model): type_name = models.CharField(max_length=200) class User(AbstractUser): user_type = models.ForeignKey(usertype,on_delete=models.CASCADE,default = 1) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.