Django custom user model doesn't show up under Authentication and Authorization in the admin page
Asked Answered
S

1

8

I have been trying to figure this out for a few hours and feel lost. I am new to django and have tried to create a custom user model. My struggle is that my user model won't show up under the Authentication and Authorization section of the admin page. Admin page pic

Here is my code for the model

from django.db import models
from django.contrib.auth.models import  AbstractUser

# Create your models here.
class User(AbstractUser):
    first_name = models.TextField(max_length = 300)
    last_name = models.TextField(max_length = 300)

Here is the code for my admin.py file

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from orders.models import User
# Register your models here.
admin.site.register(User,UserAdmin)

here is a snippet of my settings.py file where i added the AUTH_USER_MODEL

AUTH_USER_MODEL='orders.User'
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'orders'

]

Strobotron answered 2/6, 2020 at 20:17 Comment(3)
Did you find a solution? I have the same Problem. I found solutions that you can add class Meta: app_label = 'auth' But then i get an ImproperlyConfigured error.Widner
Hey! How did you fix this? I am having the same problem.Renell
You need to add this AUTH_USER_MODEL = 'applicationName.CustomUserClassName' on your settings.pyVesture
C
-1

You should not name your custom user model User. It will create issues with the base User model that you are extending.

The important thing to note is if you are using AbstractUser, then you do not need to override the username, first_name and last_name field if you're not going to do anything special with them because they are already implemented.

Your refactored User model:

from django.contrib.auth import models

class MyUser(models.AbstractUser):
    # Fields that you are not obliged to implement
    # username = models.CharField(max_length=100)
    # first_name = models.CharField(max_length=100)
    # last_name = models.CharField(max_length=100)
    groups = None
    user_permissions = None

    def __str__(self):
        return self.username

User admin:

@admin.register(MyUser)
class MyUserAdmin(admin.ModelAdmin):
    list_display = ['username']

or,

admin.site.register(MyUser, MyUserAdmin)

In settings.py you can take out the AUTH_USER_MODEL which is not necessary unless you are creating the User model from scratch.

Cutlet answered 2/6, 2020 at 21:30 Comment(2)
Hi! I've changed my custom user model's name to `Profile" and the rest is just like you wrote but I am also having the same problem. Any more recommendations?Renell
Are you sure you have registered your models correctly ?Cutlet

© 2022 - 2024 — McMap. All rights reserved.