问题描述
我正在使用Django 1.9和MySQL.我想重命名模型中的字段.让我们从 Django入门教程中查看模型Choice
.
I'm using Django 1.9 and MySQL. I want to rename a field in my model. Let's look at model Choice
from the Django getting started tutorial.
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length = 200)
votes = models.IntegerField(default=0)
因此,我想将votes
字段重命名为votes_count
.我创建了一个空迁移,并将其添加到以下行的操作中:
So, I want to rename the votes
field to votes_count
. I created an empty migration and add to operations following line:
migrations.RenameField (
model_name='choice',
old_name='votes',
new_name='votes_count',
),
在python manage.py migrate
之后,数据库表中的字段已重命名.但是当我尝试使用这种模型时
After python manage.py migrate
, the field in the database table was renamed. But when I tried to use this model
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id);
try:
selected = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question':question,
'error_message':"You didn't select a choice."
})
else:
selected.votes_count += 1
selected.save()
return HttpResponseRedirect(reverse('polls:results', args=(question_id)))
我得到了:
Traceback:
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
226. self.errorhandler(self, exc, value)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorvalue
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
217. res = self._query(query)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
378. rowcount = self._do_query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
341. db.query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
280. _mysql.connection.query(self, query)
The above exception ((1054, "Unknown column 'polls_choice.votes' in 'field list'")) was the direct cause of the following exception:
File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./polls/views.py" in vote
23. selected = question.choice_set.get(pk=request.POST['choice'])
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
848. cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/utils.py" in
__exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/utils/six.py" in reraise
685. raise value.with_traceback(tb)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
226. self.errorhandler(self, exc, value)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorvalue
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
217. res = self._query(query)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
378. rowcount = self._do_query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
341. db.query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
280. _mysql.connection.query(self, query)
Exception Type: OperationalError at /polls/1/vote/
Exception Value: (1054, "Unknown column 'polls_choice.votes' in 'field list'")
推荐答案
看起来好像您创建并运行了迁移以将模型字段从votes
重命名为votes_count
,但并未同时更新模型时间.
It looks as if you created and ran a migration to rename the model field from votes
to votes_count
, but did not update the model at the same time.
当Django尝试从数据库中获取模型时,它会尝试选择votes
列,因为您的模型中仍具有投票字段,并且由于该列在数据库中不存在而收到错误消息
When the Django tries to fetch the model from the db, it tries to select the votes
column because you still have a votes field in your models, and you get the error because the column doesn't exist in the database.
通常不需要创建手动迁移.通常,您将重命名模型字段,运行makemigrations
,然后运行migrate
.让Django创建迁移的好处是,您可以确信在运行迁移后,数据库与模型保持同步.
Creating a manual migration isn't normally necessary. Usually, you would rename the model field, run makemigrations
, then run migrate
. The advantage of letting Django create the migration is that you can be confident that the database is in sync with your models after you have run migrate.
这篇关于“字段列表"中的“"列未知. Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!