[Answered ]-Django: CustomUser model does not have the attributes of built-in User

2👍

When creating your own custom user model, it’s usually best to subclass the AbstractBaseUser which includes those default fields and will generally make the process easier on you. OneToOne relationship to the default User model is more about expnding the built-in User model, which doesn’t sound like what you’re looking for. Read more about it here

As Daniel mentioned in the comments, this means you’re going to have to explicitly include the username field (and you obviously have the freedom to define it as you’d like, it just has to be unique). In the example below I set up email to be the username

from django.contrib.auth.models import AbstractBaseUser

class CustomUser(AbstractBaseUser):
    department = models.ManyToManyField(Department)
    game = models.ManyToManyField(Game)
    # user = models.OneToOneField(User)
    email = models.EmailField(max_length=255, unique=True, db_index=True)

    USERNAME_FIELD = 'email'

Now you need to include the password field in your form, and please look at the example given in the documentation, they do a better job explaining than I do.

👤yuvi

Leave a comment