gpt4 book ai didi

python - Django:ModelChoiceField 和 django.setup()

转载 作者:行者123 更新时间:2023-11-28 21:50:12 25 4
gpt4 key购买 nike

如果我尝试通过 PyCharm 运行测试,我会得到这个异常

...bin/python /usr/local/pycharm-4.5.1/helpers/pycharm/utrunner.py ...src/foo/foo/tests/FooEditTest.py::FooEditTest::test_issue_add true
Testing started at 15:59 ...
Traceback (most recent call last):
File "/usr/local/pycharm-4.5.1/helpers/pycharm/utrunner.py", line 139, in <module>
module = loadSource(a[0])
File "/usr/local/pycharm-4.5.1/helpers/pycharm/utrunner.py", line 41, in loadSource
module = imp.load_source(moduleName, fileName)
File "...src/foo/foo/tests/FooEditTest.py", line 45, in <module>
from foo.views.issue.forward import forward
File "...src/foo/foo/views/issue/forward.py", line 29, in <module>
class ForwardForm(forms.Form):
File "...src/foo/foo/views/issue/forward.py", line 36, in ForwardForm
group=forms.ModelChoiceField(Group.objects.exclude(groupconfig__no_issue=True).extra(
File "...python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "...python2.7/site-packages/django/db/models/query.py", line 698, in exclude
return self._filter_or_exclude(True, *args, **kwargs)
File "...python2.7/site-packages/django/db/models/query.py", line 707, in _filter_or_exclude
clone.query.add_q(~Q(*args, **kwargs))
File "...python2.7/site-packages/django/db/models/sql/query.py", line 1331, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "...python2.7/site-packages/django/db/models/sql/query.py", line 1358, in _add_q
current_negated=current_negated, connector=connector)
File "...python2.7/site-packages/django/db/models/sql/query.py", line 1182, in build_filter
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "...python2.7/site-packages/django/db/models/sql/query.py", line 1120, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "...python2.7/site-packages/django/db/models/sql/query.py", line 1383, in names_to_path
field, model, direct, m2m = opts.get_field_by_name(name)
File "...python2.7/site-packages/django/db/models/options.py", line 416, in get_field_by_name
cache = self.init_name_map()
File "...python2.7/site-packages/django/db/models/options.py", line 445, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "...python2.7/site-packages/django/db/models/options.py", line 563, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "...python2.7/site-packages/django/db/models/options.py", line 577, in _fill_related_many_to_many_cache
for klass in self.apps.get_models():
File "...python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "...python2.7/site-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "...python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

Process finished with exit code 1

我使用 group=forms.ModelChoiceField(Group.objects.exclude(...))该行在导入期间执行。之前未完成对 django.setup() 的调用。

我不知道如何解决这个问题:

  • 我应该调用 django.setup() 吗?但是在哪里插入这一行呢?
  • 避免在导入期间使用 MyModel.objects.filter(...)?这需要大量重构,因为我们有多个 ModelChoiceField

最佳答案

您当前正在使用 PyCharm 的 Python 单元测试运行程序 pycharm-4.5.1/helpers/pycharm/utrunner.py 运行测试。这个测试运行器非常适合不涉及 ORM 等 Django 功能的低级单元测试(从 unittest.TestCase 子类化),但是如果测试依赖于 Django 特定的东西,比如数据库,那么你的 TestCases 可能需要从 django.test.testcases.TestCase 继承,你需要通过 PyCharm 的 Django 测试管理器 django_test_manage.py,负责设置测试数据库、初始化模型注册表等。

运行 > 编辑配置 > 添加新配置(+ 按钮)> Django 测试

将目标设置为 foo.foo.tests.FooEditTest,如果找不到您的设置,请确保将 DJANGO_SETTINGS_MODULE=... 放入环境变量中.

关于python - Django:ModelChoiceField 和 django.setup(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184369/

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