'HINT: Add or change a related_name argument to the definition for 'CustomUser.user_permissions' or 'User.user_permissions'
I need help. I'm creating an app in Django and I'm having some trouble making migrations of my custom user's class. The error is:
ERRORS: auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'Usuario.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'Usuario.groups'. auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'Usuario.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'Usuario.user_permissions'. usuarios.Usuario.groups: (fields.E304) Reverse accessor for 'Usuario.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'Usuario.groups' or 'User.groups'. usuarios.Usuario.user_permissions: (fields.E304) Reverse accessor for 'Usuario.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'Usuario.user_permissions' or 'User.user_permissions'.
Now, this is my code:
My Model:
from django.db import models from django.contrib.auth.models import AbstractUser from django.conf import settings
class CustomUser(AbstractUser):
email = models.EmailField(unique=True, max_length=80)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['USERNAME']
def __str__(self):
return self.email
class Perfil(models.Model):
user=models.OneToOneField(CustomUser, related_name="usuario_user" , on_delete=models.CASCADE)
nacionality= models.ForeignKey(Paises, on_delete=models.DO_NOTHING)
rol= models.ForeignKey(Rol, on_delete=models.DO_NOTHING) def str(self): return self.CustomUser
birth_date=models.DateField()
In my settings.py:
AUTH_USER_MODELS= 'users.CustomUser'
LOGIN_REDIRECT_URL= reverse_lazy('home')
LOGOUT_REDIRECT_URL= reverse_lazy('login')
LOGIN_URL = reverse_lazy('login')
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.users',
]
Solution 1:[1]
You can change the base model you inherited so instead of AbstractUser, just use User like:
from django.contrib.auth.models import User
class CustomUser(User):
email = models.EmailField(unique=True, max_length=80)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['USERNAME']
def __str__(self):
return self.email
take this reference https://docs.djangoproject.com/fr/4.0/topics/auth/customizing/#extending-the-existing-user-model
it will now works better. Thanks alot for question.
and in the settings.py add correctly this line:
AUTH_USER_MODEL = 'name_of_app.NameOfModel'
Solution 2:[2]
In "settings.py", use "AUTH_USER_MODEL" instead of "AUTH_USER_MODELS" as shown below:
# "settings.py"
AUTH_USER_MODEL = 'users.CustomUser'
In addition, even if you assign a tuple or list to "AUTH_USER_MODELS" as shown below, you will get the same error:
# "settings.py"
AUTH_USER_MODELS = ('users.CustomUser',) # Get the same error
Or:
# "settings.py"
AUTH_USER_MODELS = ['users.CustomUser',] # Get the same error
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
