我第一次尝试使用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:没有这样的列:users_user.user'
如果在我运行“$> python manage.py sql users
”时显示该列,该怎么办?
答案 0 :(得分:0)
manage.py sql
向您展示如果您从头开始创建表,将运行SQL 。并且syncdb
不会修改现有表。
您需要删除表并再次运行syncdb。将来,请使用South。
答案 1 :(得分:0)
我遇到了同样的问题并修复了:
class Email(models.Model):
name = models.CharField(max_length=200)
email = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
运行:
python manage.py sql emails
我得到了:
BEGIN; CREATE TABLE "emails_email" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(200) NOT NULL,
"email" varchar(200) NOT NULL,
"pub_date" datetime NOT NULL ) ;
COMMIT;
所以Django已经创建了一个主键,我们不需要创建一个主键,或者似乎我们不应该创建一个主键。