django-reversion:get_for_object()抛出数据库错误&#34;&#39; <db_name> .django_content_type&#39;不存在&#34; </db_name>

时间:2013-07-28 22:30:23

标签: mysql django django-models django-reversion

在我第一次尝试使用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中添加了特定于reversion的设置:

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',                     
...}
}

models.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吗?通过运行&#34; reversion.get_for_object(tm)&#34;我希望在发出&#34; tm.save()&#34;时,会看到一个至少包含上次保存版本的列表。在上一步中。

不确定此错误是否特定于回复处理,因为我能够提交对模型的更改,而不使用reversion。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的django_content_type表格不存在,因为django.contrib.contenttypes中已注明INSTALLED_APPS。取消注释(删除#)并再次运行syncdb。