gpt4 book ai didi

django - 操作错误: no such table: django_content_type when using unittest

转载 作者:行者123 更新时间:2023-12-02 21:12:36 27 4
gpt4 key购买 nike

我正在尝试为我的 django 项目启动单元测试,它返回以下错误:

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 模型具有对UserRankingReviewRequest 的外键“请求”。

当我为此模型设置标志 Proxy = False 时,测试运行良好,但我只需要此模型作为代理

有什么办法可以防止这种情况发生吗?

最佳答案

Proxy = True

表示没有为模型创建实际的数据库表。因此,当您尝试使用外键引用模型时,事情就会崩溃(因为外键没有可指向的数据库表)。

如果您想要实现的是拥有一个具有具体子类型的“通用抽象”类型对象,并且想要任何子类型的外键,我建议您查看 https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#generic-relations (在 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/ ) 感兴趣,它可以实现所有这些功能以及更多功能。

关于django - 操作错误: no such table: django_content_type when using unittest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com