0

P.S: I'm new to Django, hope anyone can help me out:-)

I had created a customer user model to override the already existing one Django provides, but I wanted to make a lot of changes on it including changing the name of that custom model, like completely starting over in another app but in the same project, looks like I'm getting errors while trying to do migrations.

Thought that if I changed the database and performed migrations it would work but it didn't, they say this:

ERRORS:

user.UserAccountModel.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'user.UserAccountModel.groups' clashes with reverse accessor for 'userAccount.UserModel.groups'.
        HINT: Add or change a related_name argument to the definition for 'user.UserAccountModel.groups' or 'userAccount.UserModel.groups'.
user.UserAccountModel.user_permissions: (fields.E304) Reverse accessor 'Permission.user_set' for 'user.UserAccountModel.user_permissions' clashes with reverse accessor for 'userAccount.UserModel.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'user.UserAccountModel.user_permissions' or 'userAccount.UserModel.user_permissions'.
userAccount.UserModel.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'userAccount.UserModel.groups' clashes with reverse accessor for 'user.UserAccountModel.groups'.
        HINT: Add or change a related_name argument to the definition for 'userAccount.UserModel.groups' or 'user.UserAccountModel.groups'.
userAccount.UserModel.user_permissions: (fields.E304) Reverse accessor 'Permission.user_set' for 'userAccount.UserModel.user_permissions' clashes with reverse accessor for 'user.UserAccountModel.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'userAccount.UserModel.user_permissions' or 'user.UserAccountModel.user_permissions'.

Helper: My first custom model was 'UserModel' in userAccount app and now i changed it to 'UserAccountModel' in user app

Is it maybe not allowed to change the Custom user model's name in django once you have already made migrations and established as you default user model?

Here is my models.py code:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import gettext_lazy as _

class UserManager(BaseUserManager):

def create_user(self, email, password, **extra_fields):

    if email is None:
        raise ValueError(_('Email field is required'))
    email = self.normalize_email(email)
    user = self.model(email=email, **extra_fields)
    user.set_password(password)
    user.save()
    return user

def create_superuser(self, email, password, **extra_fields):
    """
    Create and save a SuperUser with the given email and password.
    """
    extra_fields.setdefault('is_staff', True)
    extra_fields.setdefault('is_superuser', True)
    extra_fields.setdefault('is_active', True)

    if extra_fields.get('is_staff') is not True:
        raise ValueError(_('Superuser must have is_staff=True.'))
    if extra_fields.get('is_superuser') is not True:
        raise ValueError(_('Superuser must have is_superuser=True.'))
    if extra_fields.get('is_active') is not True:
        raise ValueError(_('Superuser must have is_active=True.'))
    return self.create_user(email, password, **extra_fields)

class UserAccountModel(AbstractBaseUser,PermissionsMixin):

USER_ROLE=(
    ('admin','Admin'),
    ('talent','Talent'),
    ('employer','Employer'),
)
# Talent, employer

email = models.EmailField(_('email address'),unique=True)
first_name = models.CharField(_('first name'), max_length=100)
last_name = models.CharField(_('last name'), max_length=100)
role = models.CharField(choices=USER_ROLE,max_length=10,default=USER_ROLE[2][1])
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
is_verified = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)


USERNAME_FIELD='email'
# REQUIRED_FIELDS=[]

objects = UserManager()

def __str__ (self):
    return self.email

def tokens(self):
    return ''
Juanmi Taboada
  • 539
  • 10
  • 25
Gaga
  • 1
  • 3
  • Please share your models.py file code – Nikhil Kotiya Jan 22 '23 at 11:46
  • just follow the HINT in the error message and define related_names for your model fields in the new model UserAccountModel or completely remove the first model UserModel before doing migrations for the new model. More Info e.g. here: https://stackoverflow.com/questions/2642613/what-is-related-name-used-for – Razenstein Jan 22 '23 at 12:11
  • @NikhilKotiya I added my model's code above – Gaga Jan 22 '23 at 12:16
  • @Razenstein How can I completely remove the first custom model (UserModel)? because deleting the migrations alone didn't work, it was the same issue – Gaga Jan 22 '23 at 14:23
  • I suppose you are just setting up the project and now you deleted some migration files. In this case remove Usermodel from models.py, delete all migrations, delete the database, do makemigrations and migrate again. – Razenstein Jan 22 '23 at 14:40
  • @Razenstein It is a project I cloned from github, I was working on it with my colleague so there weren't any migrations in the file for me to delete...which is why I just made them after having changed the model. – Gaga Jan 24 '23 at 11:50

0 Answers0