I want to create a custom user model using django.contrib.auth.models.AbstractUser
as stated in the djangodocs:
If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.
So I inherited the AbstractUser
class in my Users
class and added a field. But when I run the python manage.py syncdb
I get the following error:
CommandError: One or more models did not validate:
admin.logentry: 'user' has a relation with model login.users, which has either
not been installed or is abstract.
I went through other questions on stackoverflow but couldn't resolve the error. Here is my code:
models.py
from django.conf import settings
from django.db import models
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import AbstractUser
from django.contrib import admin
class Users(AbstractUser):
college = models.CharField(max_length=40)
admin.site.register(Users, UserAdmin)
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from login.models import Users
from django import forms
class UsersChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = Users
class UsersAdmin(UserAdmin):
form = UsersChangeForm
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('college',)}),
)
admin.site.register(Users, UsersAdmin)
settings.py
INSTALLED_APPS = (
'forms',
'login',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
AUTH_USER_MODEL = 'login.users'
EDIT:
I want to store the user information in the same table as auth_user
and not in a new table.
Django comes with an excellent built-in User model and authentication support. It is a primary reason most developers prefer Django over the Flask, FastAPI, AIOHttp, and many other frameworks.
The default User model in Django uses a username to uniquely identify a user during authentication. If you'd rather use an email address, you'll need to create a custom User model by either subclassing AbstractUser or AbstractBaseUser .
Every new Django project should use a custom user model. The official Django documentation says it is “highly recommended” but I'll go a step further and say without hesitation: You are straight up crazy not to use a custom user model up front.
I did this in one of my project. I was surprised to see that you extended User because the doc says something else :) You can extend Django User model, but if you only want to add new fields (not change its behavior), you should use a OneToOneField.
If you wish to store information related to User, you can use a one-to-one
relationship to a model containing the fields for additional information.
So, as you can see in the link, your code should look like:
from django.contrib.auth.models import User
class MyUser(models.Model):
user = models.OneToOneField(User)
# Or a ForeingKey to the College table?
college = models.CharField(max_length=40)
other_data = ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With