V
V
Vasily Bratushka2015-10-15 17:39:01
Django
Vasily Bratushka, 2015-10-15 17:39:01

Django. How to create a custom user without a password but with a token?

Django 1.8, python3
There was a need to create a custom user. Followed the manual https://docs.djangoproject.com/en/1.8/topics/auth/... , the user class is inherited from AbstractBaseUser
AbstractBaseUser has a "password" field that I would not like to have in the model. How to remove it?

class MyUser(AbstractBaseUser):
    token = models.CharField(unique=True, max_length=220) # need to consider Crypt
    first_name = models.CharField(null=True, blank=True, max_length=63)
    last_name = models.CharField(null=True, blank=True, max_length=63)
    password = None

so the password field doesn't disappear

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Shultais, 2015-10-15
@shultais

AbstractBaseUser has several methods for working with "empty passwords", just suitable for working with tokens.

def set_unusable_password(self):
    # Создает "ниюзабельный" пароль, который при вызове метода check_password будет возвращать False
    self.password = make_password(None)

def has_usable_password(self):
    return is_password_usable(self.password)

V
Vasily Bratushka, 2015-10-15
@emigrant90

Solved the problem radically. Instead of inheriting from AbstractBaseUser, he inherited from models.Model and added the necessary methods by hand. The only thing that might be a problem when upgrading to a new version of Django is the get_session_auth_hash() method. And I don’t think (and in vain).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question