Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove password from custom Django User model

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 = []
like image 842
Anas Aldrees Avatar asked Jan 21 '16 11:01

Anas Aldrees


2 Answers

Just override the password attribute:

password = None
like image 193
sean Avatar answered Sep 20 '22 15:09

sean


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
like image 27
Alasdair Avatar answered Sep 18 '22 15:09

Alasdair