Django 4.1: Issues with Custom User Model and Default Django Auth Forms
I'm reviewing some code and I'm not sure how to approach This might be a silly question, but Quick question that's been bugging me - I'm using Django 4.1 and have created a custom user model that extends AbstractBaseUser and implements AbstractUser for additional fields like 'bio' and 'profile_picture'..... However, when I try to use the built-in authentication forms (like `UserCreationForm` and `AuthenticationForm`), I encounter the error: 'User matching query does not exist.' This seems to occur when the form tries to validate the user during the authentication process. I've tried overriding the forms to use my custom user model by setting the `Meta.model` to my user model and providing a custom manager. Here's what my custom user model looks like: ```python from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin from django.db import models class CustomUserManager(BaseUserManager): def create_user(self, email, password=None, **extra_fields): if not email: raise ValueError('The Email field must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password=None, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) return self.create_user(email, password, **extra_fields) class CustomUser(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True) bio = models.TextField(blank=True) profile_picture = models.ImageField(upload_to='profile_pics/', blank=True) objects = CustomUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] def __str__(self): return self.email ``` In the forms, I tried this: ```python from django import forms from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from .models import CustomUser class CustomUserCreationForm(UserCreationForm): class Meta: model = CustomUser fields = ('email',) class CustomAuthenticationForm(AuthenticationForm): class Meta: model = CustomUser fields = ('email', 'password') ``` When I use these forms in my views, the registration seems to work fine, but during login attempts, the error occurs. I have ensured that `AUTH_USER_MODEL` is correctly set in my settings.py like so: ```python AUTH_USER_MODEL = 'myapp.CustomUser' ``` I've also verified that my database is migrated correctly with the custom user model. Can anyone shed some light on what could be causing this issue? Is there any other configuration I might have missed or best practices I should follow when creating a custom user model? Thanks in advance! For context: I'm using Python on Windows. My development environment is Linux. Any help would be greatly appreciated! I'm coming from a different tech stack and learning Python. Has anyone else encountered this? The project is a CLI tool built with Python. What are your experiences with this?