我对Django 1.2.4有问题。
下面是一个模型:
class Foo(models.Model):
# ...
ftw = models.CharField(blank=True)
bar = models.ForeignKey(Bar, blank=True)
在刷新数据库之后,我使用shell:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from apps.foo.models import Foo
>>> Foo.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 67, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 82, in __len__
self._result_cache.extend(list(self._iter))
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 271, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 677, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
DatabaseError: column foo_foo.bar_id does not exist
LINE 1: ...t_omg", "foo_foo"."ftw", "foo_foo...
我在这里做错什么了?
更新:如果我注释掉ForeignKey
,问题就消失了。
更新2:奇怪的是,此单元测试工作正常:
def test_foo(self):
f = Foo()
f.save()
self.assertTrue(f in Foo.objects.all())
为什么它在这里工作而不是在壳里?
更新3:它在单元测试中工作而不是在shell中工作的原因可能与正在使用的不同数据库有关:
设置.py:
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2',
'NAME': 'foo',
'USER': 'bar',
'PASSWORD': 'baz',
'HOST': '',
'PORT': '',
}
}
import sys
if 'test' in sys.argv or True:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'testdb'
}
}
更新4:确认使用SQLite3作为数据库时,一切正常。
请在删除整个数据库之前阅读此内容
我也有同样的问题。请仔细阅读例外情况。我有一个ModelForm类,它从我的表中读取数据来构建一个表单,异常就在那里。我发表了评论,然后运行makemigration并完全工作。在那之后,我评论了ModelForm类,一切都很完美。
希望这有帮助。
我通过将特定的表放到问题模型中来解决了这个问题。然后使用:
如果您使用PostgreSQL,那么我建议使用phppgadmin,它是一个类似于用于mySQL的PHPmyadmin的web界面。
除了用户界面之外,您可以只执行命令行
在d中时,按q键退出
请在运行syncdb之前尝试完全删除/清除数据库。
我记得在我对外键字段做了一些更改之后,需要这样做。
相关问题 更多 >
编程相关推荐