При входе в систему django пользователь всегда выдает ошибку, так как администратор с таким номером телефона уже существует

У меня есть пользовательская модель пользователя с полем username phone_number, унаследованным от AbstractBaseUser и PermissionMixin.I have multiple collge and each college has separate custom admin panel.I have created a admin for each college and i have created a login template to login each college admin.

view.py

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            phone_number = form.cleaned_data['phone_number']
            password = form.cleaned_data['password']
            user = authenticate(request, username=phone_number, password=password)
            print(f"phone_number: {phone_number}")
            print(f"password: {password}")
            print(f"User: {user}")
            if user is not None:
                auth_login(request, user)
                messages.success(request, "Login successful")
                return redirect('dashboard')
            else:
                messages.error(request, "Login failed. Enter valid credentials and try again.")
        else:
            messages.error(request, "Form is invalid. Please check the entered data.")
    else:
        form = LoginForm()
return render(request, 'accounts/login.html', {'form': form})

models.py

class Custom_user(AbstractBaseUser,PermissionsMixin):
    phone_number=models.CharField(max_length=13,validators=[phone_validator],unique=True)
    USERNAME_FIELD='phone_number'
    REQUIRED_FIELDS=[]
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser=models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)
    objects = Custom_user_manager()
    
    def __str__(self) -> str:
        return  self.phone_number
    class Meta:
        verbose_name = 'Admin'
        verbose_name_plural = 'Admins'

form.py

class LoginForm(forms.ModelForm):
    class Meta:
        model = Custom_user
        fields = ['phone_number', 'password']
        
        widgets={
            'password':forms.PasswordInput(),
        }
        
    def __init__(self, *args, **kwargs):
            super(LoginForm, self).__init__(*args, **kwargs)
            self.fields['phone_number'].widget.attrs.update({'class': 'form-control'})
            self.fields['password'].widget.attrs.update({'class': 'form-control'})

Когда я нажимаю на кнопку входа в систему, она выдает ошибку The admin with phone number already exist

Вернуться на верх