0

我在做 Django 教程时尝试通过管理站点访问我的应用程序并收到此错误:

DatabaseError at /admin/polls/poll/ (1054, "Unknown column 'polls_poll.question' in 'field list'")

我最初将我的模型设置为使用 Question 类,同时错误地使用了 1.6 版本的教程,但切换到 1.54 版本并且不得不将我的 Question 类重命名为 Polls。然后我重新同步了数据库。我正在使用 MySQL。

现在,每次我从管理站点单击“投票”时,都会引发上述错误。

我曾尝试刷新我的数据库并重新同步,但没有奏效。

这是我的models.py代码:

from django.db import models
import datetime
from django.utils import timezone

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self): 
        return self.question

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):

    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __unicode__(self):
        return self.choice_text

这是完整的追溯:

Internal Server Error: /admin/polls/poll/
Traceback (most recent call last):
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/options.py", line 372, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 21, in bound_func
    return func(self, *args2, **kwargs2)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1285, in changelist_view
    'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 106, in __len__
    self._result_cache = list(self.iterator())
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 317, in iterator
    for row in compiler.results_iter():
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
    return self.cursor.execute(sql, params)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 128, in execute
    six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 120, in execute
    return self.cursor.execute(query, args)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/Users/MyClean/anaconda/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
DatabaseError: (1054, "Unknown column 'polls_poll.question' in 'field list'")
4

1 回答 1

2

Django 的syncdb命令只会创建不存在的表。因此,如果您之前已将数据库与不同版本的模型同步,则字段名称的任何更改都不会出现在数据库表中。

要进行验证,请使用mysql客户端检查您的polls_poll表。

mysql> DESCRIBE polls_poll;

您可能会看到该question字段丢失。

正如评论中所建议的,您可以擦除整个数据库,但侵入性较小的解决方案只是删除polls_poll表并syncdb再次运行。

mysql> DROP TABLE polls_poll;
$ manage.py syncdb

这当然会删除与民意调查相关的所有数据。

于 2013-10-25T03:10:39.793 回答