why set password method doesn't working in django?

38 Views Asked by At

I implemented the user manager class but my set_password method only works for super users and I have problem with regular users log in

this is my manager.py:

from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
    def create_user(self, username, phone_number, password, **extra_fields):
        if not phone_number:
            raise ValueError('Phone number must be set!')
        user = self.model(username=username, phone_number=phone_number, **extra_fields)
        user.set_password(user.password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, phone_number, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(username, phone_number, password, **extra_fields)


this is my user model:

from django.db import models
# Create your models here.
from django.core.validators import RegexValidator
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import AbstractUser
from .manager import UserManager



`class User(AbstractUser):
    GENDER_CHOICE = (
        ('he', 'male'),
        ('she', 'female')
    )

    HOBBIES = (
        ('sport', ''),
        ('music', '♫'),
        ('movie', ''),
 

       ('sleeping', 'ᶻ  '),
        ('photography', '')
    )
    username = models.CharField(max_length=25, unique=True)
    phone_number = models.CharField(max_length=11, validators=[RegexValidator(
        regex=r"^09[0|1|2|3][0-9]{8}$"
    )])
    biography = models.CharField(max_length=20, default=None, null=True, blank=True)
    hobbies = models.CharField(choices=HOBBIES,max_length=250,null=True,blank=True)
    gender = models.CharField(choices=GENDER_CHOICE, max_length=250, null=True)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['phone_number', 'password']
    objects = UserManager()`



I've tried set_password mthod in my manager file but it only works for super users and it doesn't hash regular users's password and i have problem with log in for regular users because it compares the hashed password with unhashed password that saved in database and raises error i wanted to customize the password by myself but apparently you must use another name for password field in model like custom_password so i tried this and i changed the sep_password method argument with "custom_password" and it didn't work again so i removed the password field to use the password for the model itself but I still have this problem and it's my 4th project and i'm tired

0

There are 0 best solutions below