在我第一次尝试使用 django-reversion 时,我正在评估它是否可以在我的模型上执行某些基本的版本检索操作:
在将更改保存到还原范围内的特定字段后,我无法检索特定模型的先前版本列表,如下所示。尝试 reversion.get_for_object() 时,堆栈跟踪中出现以下错误:
DatabaseError: (1146, "Table 'pvtestmatrix.django_content_type' doesn't exist")
Django:v1.3.1
django-reversion:v1.5.7
安装 django-reversion 并成功管理了一个 syncdb:
bash-3.2$ python manage.py syncdb
Creating tables ...
Creating table reversion_revision
Creating table reversion_version
Installing custom SQL ...
Installing indexes ...
No fixtures found.
在 settings.py 中添加了特定于还原的设置:
INSTALLED_APPS = (
# 'django.contrib.auth',
# 'django.contrib.contenttypes',
# 'django.contrib.sessions',
# 'django.contrib.sites',
# 'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.staticfiles',
'collabgrid.testmatrix',
'collabgrid.testcase',
'collabgrid.status',
'reversion',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'reversion.middleware.RevisionMiddleware',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'PVTestMatrix',
...}
}
模型.py:
...
class Testmatrix(models.Model):
testmatrixid = models.AutoField(primary_key=True, db_column='TestMatrixId')
platform = models.CharField(max_length=60, db_column='Platform', blank=True)
...
class Meta:
db_table = u'TestMatrix'
def __str__(self):
return '%s__%s__%s' % (self.productid, self.testmatrixid, self.owner)
...
view.py 片段:
from collabgrid.testmatrix.models import Testmatrix
import reversion
reversion.register(Testmatrix)
tm=Testmatrix.objects.get(pk=729)
with reversion.create_revision():
tm.platform="AAA"
tm.save()
version_list = reversion.get_for_object(tm)
堆栈跟踪:
>>> from collabgrid.testmatrix.models import Testmatrix
>>> import reversion
>>>
>>> reversion.register(Testmatrix)
>>> tm=Testmatrix.objects.get(pk=729)
>>> with reversion.create_revision():
... tm.platform="AAA"
... tm.save()
...
Traceback (most recent call last):
File "<console>", line 3, in <module>
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 290, in __exit__
self._context_manager.end()
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 176, in end
in manager_context.iteritems()
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 175, in <genexpr>
for obj, data
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 602, in <lambda>
version_data = lambda: adapter.get_version_data(instance, VERSION_CHANGE, self._revision_context_manager._db)
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 97, in get_version_data
content_type = ContentType.objects.db_manager(db).get_for_model(obj)
File "/Library/Python/2.6/site-packages/django/contrib/contenttypes/models.py", line 38, in get_for_model
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/Library/Python/2.6/site-packages/django/db/models/manager.py", line 135, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 378, in get_or_create
return self.get(**lookup), False
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 344, in get
num = len(clone)
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 273, in iterator
for row in compiler.results_iter():
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'pvtestmatrix.django_content_type' doesn't exist")
>>> version_list = reversion.get_for_object(tm)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 527, in get_for_object
return self.get_for_object_reference(obj.__class__, obj.pk, db)
File "/Library/Python/2.6/site-packages/reversion/revisions.py", line 506, in get_for_object_reference
content_type = ContentType.objects.db_manager(db).get_for_model(model)
File "/Library/Python/2.6/site-packages/django/contrib/contenttypes/models.py", line 38, in get_for_model
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/Library/Python/2.6/site-packages/django/db/models/manager.py", line 135, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 378, in get_or_create
return self.get(**lookup), False
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 344, in get
num = len(clone)
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 273, in iterator
for row in compiler.results_iter():
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'pvtestmatrix.django_content_type' doesn't exist")
我在这里正确使用回归吗?通过运行“reversion.get_for_object(tm)”,我希望在上一步发出“tm.save()”时看到一个至少包含最后保存版本的列表。
不确定此错误是否特定于反转处理,因为我可以在不使用反转的情况下提交对模型的更改。提前致谢。