I have the following problem :
i created a custom UserModel and Djoser for jwt auth.
when i create a superuser by terminal it created with hashed password but when i create it with an APIVIEW it created without being hashed so when i need to create a token for created user it gives me "detail": "No active account found with the given credentials"
but when i create a token for the superuser it created successfully.
my custom user model:
class CustomUserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_("Please enter your email address"))
email = self.normalize_email(email)
new_user = self.model(email = email, **extra_fields)
new_user.set_password(password)
new_user.save()
return new_user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Super user should have is_staff True'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Super user should have is_superuser True'))
if extra_fields.get('is_active') is not True:
raise ValueError(_('Super user should have is_active True'))
return self.create_user(email, password, **extra_fields)
class User(AbstractUser):
username = models.CharField(max_length=200, unique=True)
email = models.EmailField(max_length=200, unique=True)
phone_number = PhoneNumberField(null=False, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'phone_number']
objects = CustomUserManager()
def __str__(self):
return self.username
User View:
class UserView(APIView):
def get(self, request):
user = User.objects.all()
serializer = UserCreationSerializer(user, many = True)
return Response(serializer.data, status = status.HTTP_200_OK)
def post(self, request):
serializer = UserCreationSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(status = status.HTTP_406_NOT_ACCEPTABLE)
Thanks in advance.
if anyone interested i finally figured it out. my main problem that i didn't have a create password function s when i create a user its password didn't get hashed. i had to add this function to my serializers.
def create(self, validated_data):
user = User.objects.create(
username = validated_data['username'],
email = validated_data['email'],
phone_number = validated_data['phone_number']
)
user.set_password(validated_data['password'])
user.save()
return user