Home > Software engineering >  Custom User Model Django Error , No such table
Custom User Model Django Error , No such table

Time:11-06

Models.py

class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        if not email:
            raise ValueError('Users must have an email address')
        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user


    def create_superuser(self, email, password):
        user = self.create_user(
            email,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)  # a admin user; non super-user
    admin = models.BooleanField(default=False)  # a superuser
    phone = models.IntegerField()
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def get_full_name(self):            
        return self.email

    def get_short_name(self):
        return self.email

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin

    objects = UserManager()

Settings.py

AUTH_USER_MODEL = 'Accounts.User'

Error:

return Database.Cursor.execute(self, query, params)                                                                 
django.db.utils.OperationalError: no such table: Accounts_user 

I have created a Model User, but when I tried to create a superuser, i got an error, that table does not exist, I have tried makemigrations and migrate command multiple times, but nothing seems to solve the issue

i can't even open the table even in the admin pannel, can someone help me solve this issue

CodePudding user response:

I try to give a couple of tips:

  1. Make sure your model is in the Accounts app you created

  2. Check if the Accounts app is registered in settings.py

CodePudding user response:

A small issue, using default command

manage.py makemigration
manage.py migrate

didn't update the Accounts App User table, so Have to Use

manage.py makemigration Accounts
manage.py migrate Accounts

This Solves the Error

  • Related