问题描述
我正在django-2.2项目中使用多种模型来运行像这样的测试:
class Person(models.Model):
name = models.CharField(max_length=200)
运行一个简单的测试:
class TestRegistration(SimpleTestCase):
def setUp(self):
self.site = admin.AdminSite()
def test_bare_registration(self):
self.site.register(Person)
self.assertisinstance(self.site._registry[Person],admin.ModelAdmin)
但是总是会得到与其他模型相同的RuntimeError类型(模型引发了更改,但RuntimeError仍然存在):
"INSTALLED_APPS." % (module,name)
RuntimeError: Model class tests.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
我已经尝试过:
- 删除 init .py
- 删除 init .py 的内容
- 从 init .py 显式导入模型
- 按照本文的建议从函数内部导入模型 (https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a)
- 使用django-nose运行测试
- 从同一测试中声明模型
- 使用相同设置设置测试环境,但添加模型
- 不仅使用-c“ python manage.py test”运行测试,还使用“ python runtest.py”作为建议 (https://docs.djangoproject.com/en/3.1/topics/testing/advanced/)
- 将“ django.contrib.sites”添加到INSTALLED_APPS
这是“已安装的应用”列表:
INSTALLED_APPS = [
# Django modules
'django.contrib.contenttypes','django.contrib.auth','django.contrib.admin','django.contrib.admindocs','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.postgres','django.contrib.gis','django.forms',# External apps
'compressor','chunked_upload','django_celery_beat','django_celery_results','djgeojson','filebrowser','leaflet','maintenance_mode','phonenumber_field','rest_framework','silk','webpack_loader',# Local apps
'ecofarm.front','ecofarm.front.ui','ecofarm.front.dashboard','ecofarm.package.base','ecofarm.package.logbook','ecofarm.package.user','ecofarm.computation','ecofarm.catalog','ecofarm.catalog.core','ecofarm.catalog.basagri','ecofarm.catalog.footprint','django_nose',
这是完整的消息错误:
Traceback (most recent call last):
File "manage.py",line 15,in <module>
execute_from_command_line(sys.argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/__init__.py",line 381,in execute_from_command_line
utility.execute()
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/__init__.py",line 375,in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/commands/test.py",line 23,in run_from_argv
super().run_from_argv(argv)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/base.py",line 323,in run_from_argv
self.execute(*args,**cmd_options)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/base.py",line 364,in execute
output = self.handle(*args,**options)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/core/management/commands/test.py",line 53,in handle
failures = test_runner.run_tests(test_labels)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/test/runner.py",line 627,in run_tests
suite = self.build_suite(test_labels,extra_tests)
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/test/runner.py",line 488,in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/lib/python3.6/unittest/loader.py",line 153,in loadTestsFromName
module = __import__(module_name)
File "/home/codea/Workspace/ecoclimasol/climfarm/tests/__init__.py",line 1,in <module>
from tests.models import *
File "/home/codea/Workspace/ecoclimasol/climfarm/tests/models.py",in <module>
class Person(models.Model):
File "/home/codea/Workspace/venv3.6/lib/python3.6/site-packages/django/db/models/base.py",line 111,in __new__
"INSTALLED_APPS." % (module,name)
RuntimeError: Model class tests.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)