0

我有一个我无法检测到的错误,我正在学习 Django,一开始我正在使用 Django 默认用户模型,但后来我需要一个自定义用户模型,所以我删除了我的 db(已删除 sqlite db 文件) ,在models.py中创建新的用户模型,然后makemigrations然后迁移它。但是,当我切换到管理页面时,我遇到了一个我无法解决的错误。我得到的错误是:

django.db.utils.OperationalError:没有这样的表:IEL_user

模型.py 文件

 class user(AbstractUser):

   is_student=models.BooleanField(default=False)
   is_teacher=models.BooleanField(default=False)

我在 settings.py 中添加了 AUTH_USER_MODEL = 'IEL.user'

错误是:

      Internal Server Error: /admin/
       Traceback (most recent call last):
     File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
     32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
     return self.cursor.execute(sql, params)
     File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
     32\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in 
     execute
     return Database.Cursor.execute(self, query, params)
    sqlite3.OperationalError: no such table: IEL_user

   The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
  File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
  32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
  response = get_response(request)
   File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
  32\lib\site-packages\django\core\handlers\base.py", line 128, in 
  _get_response
 response = self.process_exception_by_middleware(e, request)
 File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
 32\lib\site-packages\django\core\handlers\base.py", line 126, in 
 _get_response
 response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\Nabeel Ayub\AppData\Local\Programs\Python\Python35- 
32\lib\site-packages\django\contrib\admin\sites.py", line 241, in wrapper
return self.admin_view(view, cacheable)(*args, **kwargs)
4

2 回答 2

1

您可以自定义用户模型如下:

class UserManager(BaseUserManager):
    use_in_migrations = True

    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 given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', 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)
#..................................................................

from __future__ import unicode_literals

from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _

from .managers import UserManager


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
    is_active = models.BooleanField(_('active'), default=True)
    avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        '''
        Returns the first_name plus the last_name, with a space in between.
        '''
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        '''
        Returns the short name for the user.
        '''
        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):
        '''
        Sends an email to this User.
        '''
        send_mail(subject, message, from_email, [self.email], **kwargs)

重要的是,在您的 settings.py 文件中添加以下代码:

AUTH_USER_MODEL = 'IEI.User'
于 2018-08-09T12:46:49.773 回答
0

像这样迁移:

python manage.py makemigrations IEI
python manage.py migrate IEI
于 2018-08-07T09:31:33.200 回答