为什么我在django unittest中收到有关应用名称的错误?

时间:2018-01-31 17:44:08

标签: django unit-testing

运行manage.py测试时,收到以下错误。

======================================================================
ERROR: Failure: RuntimeError (Model class app.pipeline.models.Product doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/usr/local/lib/python3.5/site-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python3.5/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python3.5/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/local/lib/python3.5/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/usr/local/lib/python3.5/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 697, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/src/app/pipeline/tests.py", line 9, in <module>
    from .views import job_details
  File "/usr/src/app/pipeline/views.py", line 37, in <module>
    from .models import (Product, Platform, CdTool,
  File "/usr/src/app/pipeline/models.py", line 9, in <module>
    class Product(models.Model):
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 113, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class app.pipeline.models.Product doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

settings.py

INSTALLED_APPS = [
    'pipeline.apps.PipelineConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_nose',
    'bootstrap3',
    'bootstrap_pagination',
    'mobilereports',
    'rest_framework',
    'api'
]

此错误仅在我运行测试时被抛出,但不会以任何方式影响网站的性能(现在正在生产中)。

此项目是继承的,项目或应用程序名称可能先前已更改(不确定错误是否与此相关)。

1 个答案:

答案 0 :(得分:2)

此错误消息的原因很可能与您将__init__.pymanage.py放在同一文件夹中有关。如果您删除了__init__.py,那么测试跑步者就不会再抱怨了。

有关更多信息,请参见Django Bugtracker中的this comment

在这里添加这个,因为我在寻找问题的答案时遇到了这个问题。