Django 1.7在运行测试时加载无关的模型

2024-05-07 23:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django项目,它与Django 1.6配合得很好,但在我尝试升级到Django 1.7时给我带来了很多麻烦

在这个项目中是一个应用程序,my_app,它不包含自己的任何模型,但包含一个文件other_models.pygenerated to interact with a legacy database,使用python manage.py inspectdb。当我连接到这个辅助数据库时,我只想在特定的代码路径中使用这些模型。同样,这在Django 1.6中运行良好

升级到Django 1.7后,我无法再使用python manage.py test运行我的测试套件。当我这样做时,会出现大量错误,如下所示:

CommandError: System check identified some issues:

ERRORS:
my_app.AccountAccount.created_by_type: (fields.E304) Reverse accessor for 'AccountAccount.created_by_type' clashes with reverse accessor for 'AccountAccount.deactivated_by_type'.
    HINT: Add or change a related_name argument to the definition for 'AccountAccount.created_by_type' or 'AccountAccount.deactivated_by_type'.
...
# Tons more of these

这些错误都在抱怨other_models.py中定义的模型。在我看来changes in app-loading process是我的问题的根源,但我不完全确定

我已尝试使用以下代码在此应用程序中设置apps.py

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'my_app'
    models_module = None

并根据documentation on configuring applicationsdefault_app_config = 'my_app.apps.MyAppConfig'添加到我的my_app.__init__.py,但没有效果

我完全不知道下一步该怎么做,如果能提供关于如何控制Django何时以及如何尝试加载模型的任何信息,尤其是在运行python manage.py test


Tags: apps项目djangopy模型app应用程序for