E.Mohammed
E.Mohammed

Reputation: 163

Setting password for user model in the admin page

I created a custom User model with multiple roles customers, and employees, where employees also are in different roles: Drivers and administration.

I extended AbstractBaseUser model class to set a customized user model as this:

class UserAccountManager(BaseUserManager):
    def create_superuser(self, email, first_name, last_name, password, **other_fields):

        other_fields.setdefault("is_staff", True)
        other_fields.setdefault("is_superuser", True)
        other_fields.setdefault("is_active", True)
        other_fields.setdefault("is_driver", True)
        other_fields.setdefault("is_customer", True)
        other_fields.setdefault("is_responsable", True)

        if other_fields.get("is_staff") is not True:
            raise ValueError(_("Superuser must be assigned to is_staff."))
        if other_fields.get("is_superuser") is not True:
            raise ValueError(_("Superuser must be assigned to is_superuser."))

        return self.create_user(email, first_name, last_name, password, **other_fields)

    def create_user(self, email, first_name, last_name, password, **other_fields):

        if not email:
            raise ValueError(_("You must provide an email address"))

        email = self.normalize_email(email)
        user = self.model(email=email, first_name=first_name, last_name=last_name, **other_fields)
        user.set_password(password)
        user.save()
        return user


class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(_("Email Address"), unique=True)
    first_name = models.CharField(_("First Name"), max_length=150, unique=True)
    last_name = models.CharField(_("Last Name"), max_length=150, unique=True)
    mobile = models.CharField(_("Mobile Number"), max_length=150, blank=True)
    is_active = models.BooleanField(_("Is Active"), default=False)
    is_staff = models.BooleanField(_("Is Staff"), default=False)
    is_driver = models.BooleanField(_("Is Driver"), default=False)
    is_responsable = models.BooleanField(_("Is Responsable"), default=False)
    is_customer = models.BooleanField(_("Is Customer"), default=False)
    created_at = models.DateTimeField(_("Created at"), auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(_("Updated at"), auto_now=True)

    objects = UserAccountManager()

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["first_name", "last_name"]

    class Meta:
        verbose_name = "Account"
        verbose_name_plural = "Accounts"

    def __str__(self):
        return self.first_name

and I created two types of models the extend this model which represent each on a different role and inherit from User model:

class Employee(User):
    registration_number = models.PositiveSmallIntegerField(_("Driver Registration Number"), unique=True)
    picture = models.ImageField(
        verbose_name=_("Driver Pic"), help_text=_("Driver Identity Picture"), upload_to="images/driver/"
    )
    birth_date = models.DateField(_("Date Birth of the Driver"))
    city_id = models.ForeignKey("City", blank=True, null=True, on_delete=models.SET_NULL)
    bank_id = models.ForeignKey("Bank", blank=True, null=True, on_delete=models.SET_NULL)

    class Meta:
        verbose_name = "Employee"
        verbose_name_plural = "Employees"

    def __str__(self):
        return self.first_name + " " + self.last_name


class Customer(User):

    company_name = models.CharField(_("Company Name"), max_length=150, unique=True)
    website = models.CharField(_("Company website"), max_length=150, unique=True)
    mobile_2 = models.CharField(_("Mobile Number"), max_length=150, blank=True)
    
    class Meta:
        verbose_name = "Customer"
        verbose_name_plural = "Customers"

    def __str__(self):
        return self.first_name + " " + self.last_name

I want to register the models in the admin.py:

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    pass


admin.site.register(Customer)
admin.site.register(Employee)

the problem is that, when I try to add a user from the admin page, I can't set a password for this user, I have a password field that appear when I want to add a new user in any model, but the field seems to be as any normal InputText, the password is visible when it's tapped, and no password is registered in the database. I would like to add two type of Employee in the model.py :

class Responsable(Employee):
    responsability_type = models.CharField(max_length=4, blank=True)

    class Meta:
        verbose_name = "Responsable"
        verbose_name_plural = "Responsables"

    def __str__(self):
        return self.first_name + " " + self.last_name


class Driver(Employee):
    driving_licence = models.ImageField(
        verbose_name=_("Driver Licence"), help_text=_("Driver Licence Picture"), upload_to="images/driver_licence/"
    )
    driver_licence_expiration_date = models.DateField(_("Expiration Date for Driver Licence"))

    class Meta:
        verbose_name = "Driver"
        verbose_name_plural = "Drivers"

    def __str__(self):
        return self.first_name + " " + self.last_name

I don't know if it's a good idea to design this models for this kind of roles, I want to avoid getting multiple tables with passwords stored in it.

Upvotes: 2

Views: 233

Answers (1)

Alain Bianchini
Alain Bianchini

Reputation: 4171

Don' t use model inheritance like that, especially for the custom User model. Creates a unique model that inherits from AbstractBaseUser, that contains the type of the user and that contains all the fields that you have declared in your current tables:

class User(AbstractBaseUser, PermissionsMixin):
    class UserTypes(Enum):
        customer = ('cu', 'Customer')
        responsable = ('re', 'Responsable')
        driver = ('dr', 'Driver')

        @classmethod
        def get_value(cls, member):
            return cls[member].value[0]

    user_type = models.CharField(max_length=2, choices=[x.value for x in UserTypes])
    # Insert fields that are in common between all user types, for example:
    email = models.EmailField(_("Email Address"), unique=True)
    # Insert fields that could be None depending on the user type, for example:
    company_name = models.CharField(_("Company Name"), max_length=150, unique=True, null=True, blank=True)

Then add this in your settings:

AUTH_USER_MODEL = 'yourappname.User'

Your ModelAdmin for managing users should inherit from UserAdmin to allow password management:

@admin.register(User)
class UserAdmin(UserAdmin):
    fieldsets = ((None, {'fields': ('email', 'password', 'user_type', 'company_name')})) # Other fields showed when updating an user
    add_fieldsets = ((None, {'fields': ('email', 'password', 'user_type', 'company_name')})) # Other fields showed when creating an user

Upvotes: 1

Related Questions