Custom User Manager is not working in django

I have problem with custom user manager. It's not working properly, i mean it was working well but it stopped for some reason that i don't know (maybe i broke something).

I can create user through my register page, but if i delete whole UserManager class nothing changes. It doesnt raise errors even if it should, normalize class for email is not working etc.

What's more when i create superuser via "python manage.py createsuperuser" command everything is working fine.

Models.py

class CustomUserManager(BaseUserManager):

def _create_user(self, email, user_name, password, first_name, last_name, **extra_fields):
    if not email:
        raise ValueError("Email must be provided")
    if not password:
        raise ValueError("Password is not provided")

    user = self.model(
        email=self.normalize_email(email),
        user_name = user_name,
        first_name = first_name,
        last_name = last_name,
        **extra_fields
    )
    user.set_password(password)
    user.save(using=self._db)
    return user

def create_user(self, email, user_name, password, first_name, last_name, **extra_fields):
    extra_fields.setdefault('is_staff', True)
    extra_fields.setdefault('is_active', True)
    extra_fields.setdefault('is_superuser', False)
    return self._create_user(email, user_name, password, first_name, last_name, **extra_fields)

def create_superuser(self, email, user_name, password, first_name, last_name, **extra_fields):
    extra_fields.setdefault('is_staff', True)
    extra_fields.setdefault('is_active', True)
    extra_fields.setdefault('is_superuser', True)
    return self._create_user(email, user_name, password, first_name, last_name, **extra_fields)

class MyUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=254)
user_name = models.CharField(max_length=50, unique=True)
first_name = models.CharField(max_length=240)
last_name = models.CharField(max_length=240)
friends = models.ManyToManyField('self', blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)

objects = CustomUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['user_name', 'first_name', 'last_name']

Forms.py

class UserRegisterForm(UserCreationForm):

class Meta:
    model = MyUser
    fields = ['email', 'first_name', 'last_name', 'user_name']

Views.py

class RegisterFunction(FormView):
template_name = 'website/register.html'
redirect_authenticated_user = True
form_class = UserRegisterForm
success_url = reverse_lazy('index')
def form_valid(self, form):
    user = form.save()

    if user is not None:
        login(self.request, user)
    return super(RegisterFunction, self).form_valid(form)

def get(self, request, *args, **kwargs):
    if self.request.user.is_authenticated:
        return redirect('index')
    return super(RegisterFunction, self).get(request, *args, **kwargs)
Back to Top