I am creating a custom User model and I cant get Users table in my admin models.py:
class EmailUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('Email required')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, password, **extra_fields)
class EmailUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = EmailUserManager()
USERNAME_FIELD = 'email'
groups = models.ManyToManyField(Group, related_name="customuser_set")
user_permissions = models.ManyToManyField(Permission, related_name="customuser_set")
settings.py:
AUTH_USER_MODEL = 'jwt_auth.EmailUser'
admin.py:
from django.contrib import admin
from django.contrib.auth import get_user_model
User = get_user_model()
admin.register(User)
I already run migrations and I actually can create superuser and log in to my admin. I didnot find any solution on the internet. If anybody knows how to fix admin please help.
This problem is fairly easy to fix, and it shows that you should read the documentation a bit more precisely:
You use admin.register(User)
, but right now it's a decorator that you should apply to any admin class, like this:
# admin.py
admin.register(User)(admin.ModelAdmin)
I know it's not clear, so you can write in a more conventional way:
# admin.py
@admin.register(User)
class MyAdmin(admin.ModelAdmin):
pass
But in a normal project, just use this:
# admin.py
admin.site.register(User)
More here:
About register modeladmin on admin.site: https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#modeladmin-objects, epecially paragraph "Do you need a ModelAdmin object at all?"
About admin.register decorator: https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#the-register-decorator