Yax
Yax

Reputation: 2189

Django p.choice_set.all() returns error

I am following this tutorial. It is Django 1.6.

from django.db import models
import datetime
from django.utils import timezone
# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):  # Python 3: def __str__(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):  # Python 3: def __str__(self):
        return self.choice_text

And I have p = Poll.objects.get(pk=1) and is returning my inserted data but but running p.choice_set.all() returns the error below:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 96, in __iter__
    self._fetch_all()
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 857, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 713, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "c:\users\Yax\appdata\local\temp\easy_install-eeowbg\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\c
ursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "c:\users\Yax\appdata\local\temp\easy_install-eeowbg\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Champ 'myapp_choice.poll_id' inconnu dans field list")

How can I have this fixed? I have syncdb my models.py but still not working.

Upvotes: 1

Views: 263

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174672

syncdb doesn't modify the tables or do anything that will destroy data. This means that if you add a column, you need to delete the table and start again with syncdb.

The error is stating that you are missing a field. So you should delete the sqlite database and run syncdb again.

The easiest way to do this is with the sqlclear management command, which will print out the SQL you need to execute to reset the database for an application.

If you have the client libraries for your database installed, you can pipe the result of this command to the dbshell management command:

python manage.py dbshell < python manage.py sqlclear myapp

Upvotes: 1

Related Questions