在做Django教程时,我试图通过管理站点访问我的应用程序,并得到以下错误:
DatabaseError at /admin/polls/poll/ (1054, "Unknown column 'polls_poll.question' in 'field list'")我最初将模型设置为使用问题类,而错误地使用了教程的1.6版本,但切换到1.54版本,不得不将我的问题类重命名为轮询。然后,我重新生成了数据库。我正在使用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'")发布于 2013-10-25 03:10:39
Django的syncdb命令只创建不存在的表。因此,如果您以前已经用不同版本的模型同步过数据库,那么字段名中的任何更改都不会出现在数据库表中。
若要验证,请使用mysql客户端检查polls_poll表。
mysql> DESCRIBE polls_poll;您可能会看到缺少question字段。
正如注释中所建议的那样,您可以删除整个数据库,但较少的侵入性解决方案只是删除polls_poll表并再次运行syncdb。
mysql> DROP TABLE polls_poll;
$ manage.py syncdb当然,这将删除所有与投票相关的数据。
https://stackoverflow.com/questions/19580364
复制相似问题