I'm developing a web site with Django.
I'm using a custom user model and a custom user manager. The User class inherits from AbstractBaseUser and PermissionsMixin. The CustomUserManager inherits from BaseUserManager. This work fine: I can create superuser and othre users normally.
Can somebody help me?
Thank you in advance.
I'm using a custom user model and a custom user manager. The User class inherits from AbstractBaseUser and PermissionsMixin. The CustomUserManager inherits from BaseUserManager. This work fine: I can create superuser and othre users normally.
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from partners.models import Partner
class UserManager(BaseUserManager):
def create_user(self, name, password=None, **extra_fields):
if not name:
raise ValueError("Name is required")
user = self.model(name=name, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, name, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(name, password, **extra_fields)
class User(AbstractBaseUser, PermissionsMixin):
name = models.CharField(max_length=30, unique=True, null=False, blank=False, help_text='Username')
partner = models.OneToOneField(Partner, on_delete=models.RESTRICT, null=True, blank=True, related_name='partner')
date_joined = models. DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'name'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('user-detail', args=[str(self.id)])
class Meta:
managed = True
db_table = 'users'
ordering = ['name']But, when I create a user by mean the Admin site, the password in database is stored in plain text, instead encrypted.Can somebody help me?
Thank you in advance.
--
Adrián E. Córdoba
Adrián E. Córdoba
