I have a problem with a custom User model in my Account app and I just put.
AUTH_USER_MODEL = 'Account.User'
in my setting.py.
The problem I'm having is that when I go to the admin panel and click on a user in the Users list, I receive an error (this error is not only for the User model but also for all other models in the admin panel; I can't delete or update them).
IntegrityError at /admin/Account/user/1/delete/
FOREIGN KEY constraint failed
IntegrityError at /admin/Account/user/1/change/
FOREIGN KEY constraint failed
Also, if I try to register a user using my register view, I get this error, which I return as a response if the user already exists in the Users list.
{
"error": "Username has been already used."
}
The register view class code for checking the email and username:
if User.objects.filter(email=email).exists():
return Response(data={"error": "Email has been already used."},
status=status.HTTP_400_BAD_REQUEST)
if User.objects.filter(username=username).exists():
return Response(data={"error": "Username has been already used."},
status=status.HTTP_400_BAD_REQUEST)
My User Model:
from django.db import models
from django.contrib.auth.models import AbstractUser, Group, Permission
class User(AbstractUser):
id = models.AutoField(primary_key=True)
email = models.EmailField(unique=True)
full_name = models.CharField(max_length=255, default="")
simple_description = models.CharField(max_length=255, default="tourist")
biography = models.TextField(null=True, blank=True, default="")
profile_picture = models.ImageField(null=True, blank=True)
groups = models.ManyToManyField(Group, blank=True, related_name='account_users')
user_permissions = models.ManyToManyField(Permission, blank=True, related_name='account_users')
def __str__(self):
return self.username
My admin codes:
from django.contrib import admin
from Destination.models import Destination, DestinationImages
from Account.models import User, Profile
class DestinationImagesInline(admin.TabularInline):
model = DestinationImages
@admin.register(Destination)
class DestinationAdmin(admin.ModelAdmin):
inlines = [DestinationImagesInline]
list_display = ["id", "name", "category", "rating", "author"]
list_filter = ["category", "rating"]
search_fields = ["name", "author__username"]
sortable_by = ["id"]
fieldsets = (
("General Info", {
"fields": ("author", "name", "rating")
}),
("Details", {
"fields": ("category", "description")
}),
("Location", {
"classes": ("collapse",),
"fields": ("location",)
}),
)
class ProfileInline(admin.StackedInline):
model = Profile
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
inlines = [ProfileInline]
list_display = ['username', 'email', 'full_name', 'id', 'is_active', 'is_staff', 'is_superuser']
search_fields = ['username', 'email', 'full_name']
list_filter = ['is_staff', 'is_superuser', 'is_active']
fieldsets = (
(None, {
'fields': ('username', 'email', 'password')
}),
('Personal Info', {
'fields': ('full_name', 'simple_description', 'biography', 'profile_picture')
}),
('Permissions', {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
}),
('Important dates', {
'fields': ('last_login', 'date_joined')
}),
)
readonly_fields = ('last_login', 'date_joined')
I also checked that I used on_delete=models.CASCADE
in every field in other models that have a Foreign Key with a model
it seems you’ve created database tables with Django’s built-in User model . than you overrided the default user model with AUTH_USER_MODEL . but the model referenced by AUTH_USER_MODEL must be created in the first migration
of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.
Solution :
Delete/Drop the whole database and redo the migrations :
1- In the migrations folders of the USER app , delete all files (e.g. 001_initial.py) and pycache folder, except for init.py
2- Run python manage.py makemigrations
Then run python manage.py migrate