6

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'

]

Ben Knight
  • 61
  • 3
  • 1
    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. – Samhamsam Nov 28 '20 at 18:31
  • 1
    Hey! How did you fix this? I am having the same problem. – Öykü Jan 14 '21 at 21:30
  • You need to add this `AUTH_USER_MODEL = 'applicationName.CustomUserClassName'` on your settings.py – Luthfi May 10 '22 at 18:05

1 Answers1

-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.

K. John
  • 129
  • 1
  • 12
  • 1
    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? – Öykü Jan 14 '21 at 21:31
  • Are you sure you have registered your models correctly ? – K. John Jul 16 '21 at 21:49