gpt4 book ai didi

Django 无法导入模型

转载 作者:行者123 更新时间:2023-12-01 11:50:29 26 4
gpt4 key购买 nike

我在 xsd_messages/forms.py 中有以下内容

import xsd_training.models

class UpdateRequestForm(forms.Form):
lesson = forms.ModelChoiceField(
queryset=xsd_training.models.Lesson.objects.all())

这给出了错误:

Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/will/env/xSACdb/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/will/env/xSACdb/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/home/will/env/xSACdb/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/will/env/xSACdb/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/will/env/xSACdb/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/will/local/xSACdb/xsd_members/models.py", line 6, in <module>
from xsd_training.models import PerformedLesson
File "/home/will/local/xSACdb/xsd_training/models.py", line 8, in <module>
import xsd_messages.views
File "/home/will/local/xSACdb/xsd_messages/views.py", line 15, in <module>
from xsd_messages.forms import MailingComposeForm, UpdateRequestForm
File "/home/will/local/xSACdb/xsd_messages/forms.py", line 14, in <module>
class UpdateRequestForm(forms.Form):
File "/home/will/local/xSACdb/xsd_messages/forms.py", line 26, in UpdateRequestForm
queryset=xsd_training.models.Lesson.objects.all())
AttributeError: 'module' object has no attribute 'models'

然而,使用 shell 证明模型确实存在:

>>> import xsd_training.models
>>> xsd_training.models.Lesson.objects.all()
[<Lesson...

这是怎么回事?

最佳答案

你有一个循环引用:members.models 导入 training.models,导入 messages.views,导入 mesages.forms,导入 training.models...循环无法解决,因此 Python 报告一个错误。

你需要打破这个链条。在没有看到代码的情况下,我无法为您提供更多帮助,但是模型文件导入 View 文件是非常可疑的:这真的不应该发生。

关于Django 无法导入模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27042178/

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