Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 2 months ago by CosmicPathfinder892

Error Creating New User with a Custom Django User Model Including 'age' Field

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm new to Django and trying to set up a custom user model with an additional 'age' field. I've configured my project to use the custom model (accounts.CustomUser) and created corresponding forms and admin registration. However, after running makemigrations for the accounts app and migrating, I encounter an error when creating a new user. My setup is as follows:

config/settings.py:

PYTHON
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'accounts', ] AUTH_USER_MODEL = 'accounts.CustomUser'

accounts/models.py

PYTHON
from django.db import models from django.contrib.auth.models import AbstractUser class CustomUser(AbstractUser): age = models.PositiveIntegerField(null=True, blank=True)

accounts/form.py

PYTHON
from django.contrib.auth.forms import UserCreationForm, UserChangeForm from .models import CustomUser class CustomUserCreationFrom(UserCreationForm): class Meta: model = CustomUser fields = UserCreationForm.Meta.fields + ('age', ) class CustomUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): model = CustomUser fields = UserChangeForm.Meta.fields

accounts/admin.py:

PYTHON
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from .models import CustomUser from .forms import CustomUserChangeForm, CustomUserCreationFrom class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationFrom form = CustomUserChangeForm model = CustomUser list_display = ['username', 'email', 'age', 'is_staff'] fieldsets = UserAdmin.fieldsets + ( ( None, {'fields': ('age', )}, ), ) add_fieldsets = UserAdmin.add_fieldsets + ( ( None, {'fields': ('age', )}, ), ) admin.site.register(CustomUser, CustomUserAdmin)

Django version: Django==5.1.4
App name: accounts

I'm looking for guidance on what might be causing this error during user creation and how to fix it.

1

Comments 0

Answers 0

Views 62

0 Answers

Discussion

No comments yet.