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)
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',
),
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)))
I got:
Unknown column 'polls_choice.votes' in 'field list
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'")
See Question&Answers more detail:
os