OperationalError:使用unittest时没有这样的表:django_content_type

时间:2014-07-29 12:30:44

标签: django unit-testing proxy inline-formset

我正在尝试为我的django项目启动unittest,它会让我失误:

ERROR: varys.users.tests.test_all_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: varys.users.tests.test_all_views
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/users/tests/__init__.py", line 1, in <module>
    from .test_all_views import *
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/users/tests/test_all_views.py", line 12, in <module>
    from ...urls import urlpatterns
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/urls.py", line 18, in <module>
    url(r'^survey/', include('varys.surveys.urls')),
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 26, in include
    urlconf_module = import_module(urlconf_module)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/surveys/urls.py", line 13, in <module>
    from .userrankingreview.views import UserRankingReviewView
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/surveys/userrankingreview/views.py", line 6, in <module>
    from .forms import UserRankingReviewFormset
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/surveys/userrankingreview/forms.py", line 63, in <module>
    formfield_callback=formfield_callback
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/forms/models.py", line 981, in inlineformset_factory
    FormSet = modelformset_factory(model, **kwargs)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/forms/models.py", line 813, in modelformset_factory
    labels=labels, help_texts=help_texts, error_messages=error_messages)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/forms/models.py", line 528, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/forms/models.py", line 282, in __new__
    opts.help_texts, opts.error_messages)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/forms/models.py", line 205, in fields_for_model
    formfield = formfield_callback(f, **kwargs)
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/surveys/userrankingreview/forms.py", line 54, in formfield_callback
    return field.formfield()
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1263, in formfield
    'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to),
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/manager.py", line 226, in using
    return self.get_queryset().using(*args, **kwargs)
  File "/home/antonscherbatov/projects/varys/locus-web-varys-d/varys/surveys/models.py", line 199, in get_query_set
    qs = qs.filter(**self.model.subclasses_lookup())
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/polymodels/models.py", line 59, in subclasses_lookup
    query_name=query_name
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/polymodels/models.py", line 50, in content_type_lookup
    value = [ct.pk for ct in get_content_types(models).values()]
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/polymodels/utils.py", line 26, in get_content_types
    return manager.get_for_models(*models, for_concrete_models=False)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 79, in get_for_models
    for ct in cts:
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/antonscherbatov/projects/varys/ve/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 451, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: no such table: django_content_type

昨天所有测试都运行良好。检查后,我发现问题出现在下面的代码中

# forms.py

UserRankingReviewFormset = inlineformset_factory(
    UserRankingReviewRequest,
    UserRankingReview,
    formset=UserRankingInlineFormSet,
    extra=0,
    can_delete=False,
    formfield_callback=formfield_callback
)

其中parent_model UserRankingReviewRequest是代理模型,UserRankingReview模型有外键&#39;请求&#39;到UserRankingReviewRequest。

当我为此模型设置标志 Proxy = False 时,测试工作正常,但我需要此模型仅作为代理

我怎么能阻止这个?

2 个答案:

答案 0 :(得分:1)

Proxy = True

表示没有为模型创建实际的数据库表。因此,当您尝试使用ForeignKey引用模型时,事情会中断(因为ForeignKey没有要指向的db表)。

如果你想要实现的是拥有一个&#34;通用摘要&#34;具有具体子类型的类型对象,并希望外键到我建议检出https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#generic-relations的任何子类型(在ForeignKey到ContentType https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to上使用自定义limit_choices_to)

您可能也对Django Polymorphic(https://django-polymorphic.readthedocs.org/en/latest/)感兴趣,它可以完成所有这些工作。

答案 1 :(得分:1)

如果在测试中发生这种情况,就像对我一样, 也许this链接 可以帮忙,简而言之答案是:

  

好的,我找到了解决问题的办法,万一有人也有同样的问题   问题。如果您在设置中指定TEST_DATABASE_NAME,则会   强制sqllite使用文件数据库而不是内存数据库,   然后问题就出现了。