[Django]-How can i use 'set_password' in custom non-admin model?

4👍

This is a really easy fix. Just change your models.py file like so:

from django.contrib.auth.models import AbstractBaseUser

class User(AbstractBaseUser):
    first_name = models.CharField(max_length=50, blank=True)
    last_name = models.CharField(max_length=50, blank=True)
    profile_picture = models.ImageField(upload_to="user_data/profile_picture", blank=True)
    username = models.CharField(max_length=100)
    birth_date = models.DateField(blank=True)
    gender = models.CharField(max_length=10, blank=True)
    password = models.CharField(max_length=1000)
    contact = models.CharField(max_length=10, blank=True)
    email = models.CharField(max_length=100)
    time_stamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.username

That way, your user model will inherit all of the AbstractBaseUser methods, including set_password.

Look at this full example from the documentation for extra information.

Leave a comment