Remove password from custom Django User model
Asked Answered
O

2

14

How to remove the password in the users table and User class in a custom user model?

I'm using django-rest-framework token authentication, so i don't need the password field.

Here is my model:

class CustomUserManager(BaseUserManager):

    def create_user(phone, name=None):
        return User.objects.create(
            name=name, phone=phone)

    def create_superuser(name, phone=None):
        pass

class User(AbstractBaseUser):
    """
    Custom django User model.
    """
    name = models.CharField(max_length=30,
                        null=True, validators=[validate_name])

    phone = PhoneNumberField(unique=True, blank=False, null=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'phone'
    REQUIRED_FIELDS = []
Oireachtas answered 21/1, 2016 at 11:23 Comment(4)
Are you sure you don't need passwords at all ? Like, say, for having root access to the admin ???Garnettgarnette
Yes i'm sure, and I don't want admin access.Oireachtas
Can we have admin access without password?? Like using OTP or something else?Variant
Were you able to remove it? How did you remove the password requirement when adding users in admin? I removed password in add_fieldsets but I'm getting Please correct the errors below. errorSheasheaf
W
25

An alternative to removing the password field would be to use set_unusable_password, which marks the user as having no password set.

def create_user(phone, name=None):
    user = User(name=name, phone=phone)
    user.set_unusable_password()
    user.save()
    return user
Winnipegosis answered 21/1, 2016 at 12:9 Comment(1)
This, as it says in the docs .. "You may need this if authentication for your application takes place against an existing external source such as an LDAP directory."Ariellearies
L
10

Just override the password attribute:

password = None
Louisalouisburg answered 21/1, 2016 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.