我有一个无法检测到的错误,我正在学习Django,一开始我正在使用Django默认用户模型,但是随后我需要使用自定义用户模型,因此我删除了db(删除了sqlite db文件),在models.py中创建新的用户模型,然后进行makemigrations然后进行迁移。但是,当我切换到管理页面时,遇到了无法解决的错误。我得到的错误是:
django.db.utils.OperationalError:没有这样的表:IEL_user
Models.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)
答案 0 :(得分:0)
像这样迁移:
python manage.py makemigrations IEI
python manage.py migrate IEI
答案 1 :(得分:0)
您可以按以下方式自定义用户模型:
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'