【发布时间】:2014-02-12 10:27:06
【问题描述】:
我是第一次尝试 Django,但在从我的模型中查询时遇到了一些问题。
这是我的模型:
class User(models.Model):
user = models.CharField(max_length=128, primary_key=True)
password = models.CharField(max_length=128)
count = models.IntegerField(default=1)
当我运行 $> python manage.py sql users 时,我得到:
BEGIN;
CREATE TABLE "users_user" (
"user" varchar(128) NOT NULL PRIMARY KEY,
"password" varchar(128) NOT NULL,
"count" integer NOT NULL
)
;
当我通过 $> python manage.py shell 启动交互式 shell 时,我在尝试检索我的用户对象时收到错误消息。我知道没有,但它应该返回一个空列表。
$> python manage.py shell
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from users.models import User
>>> User.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 71, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 96, in __iter__
self._fetch_all()
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
self._result_cache = list(self.iterator())
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 782, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 450, in execute
return Database.Cursor.execute(self, query, params)
OperationalError: no such column: users_user.user
>>>
调用>>> User.objects.all()(通过>>> from users.models import User 导入后)时,我收到一条错误消息:
'OperationalError: no such column: users_user.user'
如果我运行 '$> python manage.py sql users' 时出现该列,怎么会这样?
【问题讨论】:
-
你演过
python manage.py syncdb吗? -
@PriyankPatel 是的,我有,多次。甚至
python manage.py flush。