Home > OS >  Not able to migrate for my custom user model
Not able to migrate for my custom user model

Time:06-20

Hey guys I am having this problem with my django migrations when I am trying to create a custom user model for my website

So what is happening is when I run migrations it throws an error on me saying:-

ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'user.newuser', but app 'user' doesn't provide model 'newuser'. The field user.UserInfo.user was declared with a lazy reference to 'user.newuser', but app 'user' doesn't provide model 'newuser'.

I tried my best to try and debug it but it nothing is working i also did follow a answer on stack overflow but that also did not work I have implemented everything that is needed so yea the rest is the rest, and here is my code along with the migrations file

models:-

class CustomAccountManager(BaseUserManager):

    def create_user(self, first_name, last_name, phone_number, email, password, 
**other_fields):

        if not email:
            raise ValueError(_('You must provie an email address'))

        if not first_name:
            raise ValueError(_('You must provide a first name'))

        if not last_name:
            raise ValueError(_('You must provide a last name'))

        email = self.normalize_email(email)
        user = self.model(email=email, first_name=first_name, last_name=last_name, 
phone_number=phone_number, **other_fields)
        user.set_password(password)
        return user

    def create_superuser(self, email, first_name, last_name, phone_number, password, 
**other_fields):
        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)

        if other_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must be assigned to is_staff=True'))

        if other_fields.get('is_active') is not True:
            raise ValueError(_('Superuser must be assigned to is_active=True'))

        if other_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must be assigned to is_superuser=True'))

        self.create_user(email, first_name, last_name, password, phone_number, 
**other_fields)

class NewUser(AbstractBaseUser, PermissionsMixin):
    first_name = models.CharField(max_length=200, null=True, blank=True)
    last_name = models.CharField(max_length=200, null=True, blank=True)
    email = models.EmailField(max_length=200, null=True, blank=True, unique=True)
    password = models.CharField(max_length=200,null=True, blank=True)
    confirm_password = models.CharField(max_length=200, null=True, blank=True)
    phone_number = models.CharField(max_length=10,null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    pin_code = models.CharField(null=True, max_length=6, blank=True)
    region = models.CharField(null=True, max_length=200, blank=True)
    state = models.CharField(null=True, max_length=100, blank=True)
    start_date = models.DateField(default=timezone.now)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'phone_number']

    def __str__(self):
        return self.first_name   ' '   self.last_name

Migrations:-

class Migration(migrations.Migration):

dependencies = [
    ('auth', '0012_alter_user_first_name_max_length'),
    ('user', '0015_delete_newuser'),
]

operations = [
    migrations.CreateModel(
        name='NewUser',
        fields=[
            ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
            ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
            ('first_name', models.CharField(blank=True, max_length=200, null=True)),
            ('last_name', models.CharField(blank=True, max_length=200, null=True)),
            ('email', models.EmailField(blank=True, max_length=200, null=True, unique=True)),
            ('password', models.CharField(blank=True, max_length=200, null=True)),
            ('confirm_password', models.CharField(blank=True, max_length=200, null=True)),
            ('phone_number', models.CharField(blank=True, max_length=10, null=True)),
            ('address', models.TextField(blank=True, null=True)),
            ('pin_code', models.CharField(blank=True, max_length=6, null=True)),
            ('region', models.CharField(blank=True, max_length=200, null=True)),
            ('state', models.CharField(blank=True, max_length=100, null=True)),
            ('start_date', models.DateField(default=django.utils.timezone.now)),
            ('is_staff', models.BooleanField(default=False)),
            ('is_active', models.BooleanField(default=False)),
            ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
            ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
        ],
        options={
            'abstract': False,
        },
    ),
]

settings:-

apps-

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home.apps.HomeConfig',
'pages.apps.PagesConfig',
'user.apps.UserConfig',
'reservation.apps.ReservationConfig',
]

calling the new user model-

AUTH_USER_MODEL = 'user.NewUser'

CodePudding user response:

Changing the user model mid-project is a difficult situation if you are trying to override the existing user model with a NewUser. Reference You will need to write a custom migration to do it to transfer all data from the current User table to the NewUser table, then drop the User table safely.

If that's not the case and you just started the project, feel free to drop your current database and make migrations again after changing the default user model in settings.py (you are doing that already). Also make sure all imports in other files are undated to use your NewUser model instead of the django default User.

  • Related