Django - OperationalError 1054,"未知列' phpbb_users.password'在'字段列表'"

时间:2015-07-02 18:26:56

标签: django django-1.7

我正在尝试创建一个用于我的旧用户表的自定义用户模型。密码字段名为user_password。我尝试覆盖AbstractBaseUser的password字段并添加db_column='user_password

[field for field in MyUser._meta.fields if field.name == "password"][0].db_column = "user_password" 在末尾。这是我的模特:

class MyUser(AbstractBaseUser, PermissionsMixin):

user_id = models.IntegerField(primary_key=True)
is_active = models.BooleanField(_('active'), default=True, db_column='user_active',
    help_text=_('Designates whether this user should be treated as '
                'active. Unselect this instead of deleting accounts.'))
username = models.CharField(max_length=25, unique=True)
# user_password = models.CharField(max_length=128, verbose_name='password', db_column=b'user_password')
user_session_time = models.IntegerField(default=0)
user_session_page = models.IntegerField(default=0)
user_lastvisit = models.IntegerField(default=0)
user_regdate = models.IntegerField(default=0)
user_level = models.IntegerField(blank=True, null=True)
user_posts = models.IntegerField(default=0)
user_timezone = models.DecimalField(max_digits=5, decimal_places=2, default=0.00)
user_style = models.IntegerField(blank=True, null=True)
user_lang = models.CharField(max_length=255, blank=True)
user_dateformat = models.CharField(max_length=14, default='d M Y H:i')
user_new_privmsg = models.IntegerField(default=0)
user_unread_privmsg = models.IntegerField(default=0)
user_last_privmsg = models.IntegerField(default=0)
user_emailtime = models.IntegerField(blank=True, null=True)
user_viewemail = models.IntegerField(blank=True, null=True)
user_attachsig = models.IntegerField(blank=True, null=True, default=1)
user_allowhtml = models.BooleanField(blank=True, default=True)
user_allowbbcode = models.BooleanField(blank=True, default=True)
user_allowsmile = models.BooleanField(blank=True, default=True)
user_allowavatar = models.BooleanField(default=True)
user_allow_pm = models.BooleanField(default=True)
user_allow_viewonline = models.BooleanField(default=True)
user_notify = models.BooleanField(default=False)
user_notify_pm = models.BooleanField(default=True)
user_popup_pm = models.BooleanField(default=False)
user_rank = models.IntegerField(blank=True, null=True)
user_avatar = models.CharField(max_length=100, blank=True)
user_avatar_type = models.IntegerField(default=0)
email = models.EmailField(max_length=255, blank=True, db_column='user_email')
user_icq = models.CharField(max_length=15, blank=True)
user_website = models.CharField(max_length=100, blank=True)
user_from = models.CharField(max_length=100, blank=True)
user_sig = models.TextField(blank=True)
user_sig_bbcode_uid = models.CharField(max_length=10, blank=True)
user_aim = models.CharField(max_length=255, blank=True)
user_yim = models.CharField(max_length=255, blank=True)
user_msnm = models.CharField(max_length=255, blank=True)
user_occ = models.CharField(max_length=100, blank=True)
user_interests = models.CharField(max_length=255, blank=True)
user_actkey = models.CharField(max_length=32, blank=True)
user_newpasswd = models.CharField(max_length=32, blank=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
is_staff = models.BooleanField(_('staff status'), default=False,
    help_text=_('Designates whether the user can log into this admin '
                'site.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now) #regdate

class Meta:
    managed = True
    db_table = 'phpbb_users'
    verbose_name = 'User'
    verbose_name_plural = 'Users'


objects = UserManager()

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']

def get_full_name(self):
    return '%s, %s'  % (self.username, self.user_email)

def get_short_name(self):
    return self.username

def __str__(self):
    return self.username

以下之后,
[field for field in MyUser._meta.fields if field.name == "password"][0].db_column = "user_password"

我无法createasuperuser甚至只能runserver。我得到以下stacktrace:

Traceback:
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  104.                     response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/mezzanine/core/middleware.py" in process_view
  89.         if request.user.is_superuser:
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/utils/functional.py" in inner
  224.             self._setup()
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/utils/functional.py" in _setup
  357.         self._wrapped = self._setupfunc()
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in <lambda>
  22.         request.user = SimpleLazyObject(lambda: get_user(request))
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py" in get_user
  10.         request._cached_user = auth.get_user(request)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in get_user
  159.             user = backend.get_user(user_id)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/contrib/auth/backends.py" in get_user
  69.             return UserModel._default_manager.get(pk=user_id)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  92.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  351.         num = len(clone)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  122.         self._fetch_all()
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  966.             self._result_cache = list(self.iterator())
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  265.         for row in compiler.results_iter():
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  701.         for rows in self.execute_sql(MULTI):
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  787.             cursor.execute(sql, params)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  81.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
  129.             return self.cursor.execute(query, args)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
  205.             self.errorhandler(self, exc, value)
File "/home/pirave/work/dev_site/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: OperationalError at /admin/
Exception Value: (1054, "Unknown column 'phpbb_users.password' in 'field list'")

为什么它无法识别数据库中的user_password列? Makemigration检测到{My}的db_column更改并显示在0001_initial.py

1 个答案:

答案 0 :(得分:0)

[field for field in AbstractBaseUser._meta.fields if field.name == "password"][0].db_column = "user_password"

在课前,似乎解决了这个问题。