5

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',
]
Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
RostenRoss
  • 81
  • 2
  • 3

3 Answers3

3

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'
0

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
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

I got the same issue, try this. \\\ groups = models.ManyToManyField(Group, related_name='customuser_set', blank=True) user_permissions = models.ManyToManyField(Permission, related_name='customuser_set', blank=True) \\

[enter code here][1]
starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '23 at 13:53