leedjango
leedjango

Reputation: 411

Django ValueError: Related model 'api.User' cannot be resolved

I am going to use the django custom user model. After writing the code, I migrate it on db, and the following error appears.

ValueError: Related model 'api.User' cannot be resolved

What's problem on my code ? Here is my code.

models.py

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

class UserManager(BaseUserManager):
    """
    A custom user manager to deal with emails as unique identifiers for auth
    instead of usernames. The default that's used is "UserManager"
    """
    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            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):
        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)

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(unique=True, null=True)
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    USERNAME_FIELD = 'email'
    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

class userDetail (models.Model) :
    nickname = models.CharField(max_length=10)
    profile = models.ImageField(default='media/default_image.jpeg')

And I am at settings.py AUTH_USER_MODEL = 'api.User' also added. How can I solve it ? thanks.

Upvotes: 1

Views: 1301

Answers (3)

Sheraz Asghar
Sheraz Asghar

Reputation: 37

I was stuck at this for so long but this can be solved by

python manage.py migrate api
python manage.py makemigrations
python manage.py migrate

Upvotes: 0

Sumithran
Sumithran

Reputation: 6555

I had the same problem while migrating the model having the AbstractUser, and my user model config was AUTH_USER_MODEL = 'api.User'

Fixed the issue by migrating the models with the app name python manage.py migrate app_name

Upvotes: 1

Safiul Kabir
Safiul Kabir

Reputation: 164

To use a custom user model in Django, you need to make sure the following:

  1. api is the name of your app,
  2. api is added in INSTALLED_APPS list inside settings.py,
  3. User model is inside the api app

Upvotes: 2

Related Questions