djangodjango-rest-frameworkdjango-custom-user

Django Custom User Model migration return error : ValueError: Related model 'user_profile.customuser' cannot be resolved


I have modified the the user authentication to create a custom user. In my user_profile model I log in with email instead of username. I have extended AbstractUserModel for this purpose and have implemented manager and serializer as mentioned bellow.

My makemigrations command is executes with success but when I run migration I get an error.

I had deleted the entire database and started migrations afresh. As the project is in development I have option to manipulate database as needed.

Environment Python==3.9.1 Django==3.2.3 djangorestframework==3.12.4

user_profiles

manager.py

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _

class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if email is None:
            raise ValueError(_('The Email must be set'))
        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.'))
        return self.create_user(email, password, **extra_fields)

models.py

from django.db import models
from .manager import CustomUserManager
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
class CustomUser(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)
    ...
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email

serializer.py

from .models import CustomUser
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = CustomUser
        exclude = '__all__'
        extra_kwargs = {
            "password": {"write_only": True},
        }

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'user_profile',
    ...
]
DJOSER = {
    ...
    'LOGIN_FIELD': 'email', #Default: User.USERNAME_FIELD where User is the model set with Django’s setting AUTH_USER_MODEL.
     'SERIALIZERS': {
         'user': 'user_profile.serializer.UserSerializer',
     },
}
AUTH_USER_MODEL = 'user_profile.CustomUser'

Error :

(api) PS C:\Users\...\api> python.exe .\manage.py makemigrations         
Migrations for 'user_profile':
  user_profile\migrations\0003_auto_20210731_0838.py
    - Create model CustomUser
    - Delete model UserProfileModel
(api) PS C:\Users\...\api> python.exe .\manage.py migrate       
Operations to perform:
  Apply all migrations: activity_log, admin, audit_trail, auth, authtoken, contenttypes, django_apscheduler, guardian, otp_hotp, otp_static, otp_totp, sensor_data, sensor_details, sessions, user_profile
Running migrations:
  Applying activity_log.0001_initial... OK
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying user_profile.0001_initial... OK
  Applying admin.0001_initial...Traceback (most recent call last):
  File "C:\Users\...\api\manage.py", line 22, in <module>
    main()
  File "C:\Users\...\api\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\...\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line      
    utility.execute()
  File "C:\Users\...\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\...\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\...\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\...\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\...\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\...\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\...\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\...\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\...\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\...\lib\site-packages\django\db\migrations\operations\models.py", line 92, in database_forwards        
    schema_editor.create_model(model)
  File "C:\Users\...\lib\site-packages\django\db\backends\base\schema.py", line 329, in create_model
    sql, params = self.table_sql(model)
  File "C:\Users\...\lib\site-packages\django\db\backends\base\schema.py", line 162, in table_sql
    definition, extra_params = self.column_sql(model, field)
  File "C:\Users\...\lib\site-packages\django\db\backends\base\schema.py", line 215, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 1004, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 1001, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 897, in target_field
    return self.foreign_related_fields[0]
  File "C:\Users\...\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 644, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "C:\Users\...\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 632, in related_fields
    return self.resolve_related_fields()
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 936, in resolve_related_fields
    related_fields = super().resolve_related_fields()
  File "C:\Users\...\lib\site-packages\django\db\models\fields\related.py", line 615, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'user_profile.customuser' cannot be resolved

Solution

    1. Delete the database
    2. Delete all migration files
    3. Run makemigrations
    4. Run migrate

    Hope, this works.