Fixing the Django AUTH_USER_MODEL Configuration Issue
Based on your project structure and error message, there are several issues that need to be addressed to resolve the AUTH_USER_MODEL refers to model 'users.User' that has not been installed
error.
Main Issues
- App Configuration: The way you've registered your app in
INSTALLED_APPS
doesn't match your project structure
- Path Resolution: Your environment setup in factories.py may have incorrect paths
- Import Order: There's a circular import issue in your factories.py
Solutions
1. Fix INSTALLED_APPS Configuration
Your app is inside the api
package, so you need to register it with its full path:
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Your app with the correct path
'api.users', # Not just "users"
]
AUTH_USER_MODEL = "users.User" # This might need to be "api.users.User"
2. Fix Path Resolution in factories.py
Your path resolution seems overly complex and might be incorrect:
# factories.py
import os
import django
from pathlib import Path
from dotenv import load_dotenv
# Simpler path resolution
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Load environment variables
dotenv_path = BASE_DIR / 'my_project/.env'
load_dotenv(dotenv_path=dotenv_path)
# Set Django settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
# Initialize Django
django.setup()
import factory
from django.contrib.auth import get_user_model
User = get_user_model()
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
3. Fix Import Order and Circular Imports
Remove the direct import of User model before django.setup():
# Don't do this before django.setup()
# from users.models import User # Remove this line
4. Check Your models.py
Make sure your User model is properly defined:
# users/models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
# Your custom fields here
pass
5. Fix populate_db.py
Update your import path:
# populate_db.py
import pandas as pd
from api.users.factories import UserFactory # Use the full path
def populate_from_csv(file_path):
# Read the CSV file
data = pd.read_csv(file_path)
# Iterate over each row and create a user instance
for _, row in data.iterrows():
UserFactory(
username=row['username'],
email=row['email'],
# other fields
)
print("Database successfully populated!")
# Run the script
path = "path to the csv file"
if __name__ == "__main__":
populate_from_csv(path)
Additional Recommendations
-
Use Django's Management Commands: Instead of running scripts directly, consider creating a Django management command which avoids many setup issues.
-
Check App Configuration: Make sure your apps.py
has the correct app name:
# users/apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.users' # Full path to your app
- Run Migrations: Ensure you've run migrations for your custom user model:
python manage.py makemigrations
python manage.py migrate
These changes should help resolve the issue with your custom user model not being recognized.