在AWS上使用mod_wsgi的Django站点无法创建FK表单字段,因为尚未加载相关模型

mba*_*iel 4 django mod-wsgi amazon-web-services

我正在使用Django 1.4官方版本在Django网站上工作.我的网站有一些应用程序.其中一个应用程序有一个名为CampaignFKs的模型,用于其他应用程序中的模型.如在参考的Django(https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey)建议,我选择来定义FK字段使用字符串来代替相关的模型类本身,因为我希望在下一个版本中有循环引用,这种方法避免了循环导入问题.

当我使用部署在AWS(亚马逊网络服务)网站的BitNami djangostack 1.4(阿帕奇,mod_wsgi的,MySQL的),我部署的站点工作正常的大部分.在显示Campaign模型表单的页面上,Django在尝试创建依赖于Campaign模型的外键字段的表单字段时引发了异常,抱怨相关模型未加载.有趣/可怕的是,当我设置settings.DEBUG为True(绝对不是我们在网站上线后我们想要的东西),问题不再发生!

当我在我的本地Django开发服务器上测试它时,该网站工作得很好,并且它也使用部署在我的Windows工作站上的相同BitNami djangostack完美地工作.

这是我在AWS控制台上看到的相关Apache错误输出.

[error] ERROR :: Internal Server Error: /campaigns/
[error] Traceback (most recent call last):
[error]   File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/handlers/base.py", line 101, in get_response
[error]     request.path_info)
    ... (django/wsgi blah blah)
[error]   File "/opt/bitnami/apps/django/django_projects/Project/campaign/views.py", line 5, in <module>
[error]     from forms import CampaignForm
[error]   File "/opt/bitnami/apps/django/django_projects/Project/campaign/forms.py", line 12, in <module>
[error]     class CampaignForm(forms.ModelForm):
[error]   File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/forms/models.py", line 206, in __new__
[error]     opts.exclude, opts.widgets, formfield_callback)
[error]   File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/forms/models.py", line 160, in fields_for_model
[error]     formfield = f.formfield(**kwargs)
[error]   File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/fields/related.py", line 1002, in formfield
[error]     (self.name, self.rel.to))
[error] ValueError: Cannot create form field for 'reward' yet, because its related model 'reward.Reward' has not been loaded yet
Run Code Online (Sandbox Code Playgroud)

所以,这是一个快速回顾:

  1. 无论settings.DEBUG值如何,我的站点都可以在我的本地Django开发服务器上运行
  2. 使用我本地Windows机器上的BitNami堆栈,无论settings.DEBUG值如何,它都可以正常工作
  3. 使用AWS上的BitNami堆栈(Ubuntu),它可以使用DEBUG = True 但不能使用DEBUG = False

我理解错误意味着什么,但我不明白它为什么会发生,即为什么没有加载依赖模型.有没有人遇到类似的问题,或者有什么建议可以帮助我修复它?

注意:我尝试向Google发送错误消息,但我发现的只是引发该错误的Django源代码.我也尝试过搜索更常见的查询mod_wsgi django related model,但我找不到任何与我的问题相关的内容.

mba*_*iel 7

好吧,我在Graham Dumpleton的博客文章中找到了解决我问题的方法(http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html).

简而言之,Django开发服务器在启动时验证模型(它解决了基于字符串的关系),并且当在Ubuntu上使用DEBUG = False的BitNami djangostack下使用mod_wsgi时,可能没有完成该操作.非常具体的条件,我知道 - 但G. Dumpleton的'固定'mod_wsgi代码为我解决了这个问题.

这就是我的wsgi.py现在的样子:

#wsgi.py

#make sure the folder containing the apps and the site is at the front of sys.path
#maybe we could just define WSGIPythonPath in django.conf file...?
project_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 
if project_root not in sys.path:
    sys.path.insert(0, project_root)

#set the DJANGO_SETTINGS_MODULE environment variable (doesn't work without this, despite what G. Dumpleton's blog post said)
site_name = os.path.basename(os.path.dirname(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.settings" % site_name)

#new code - fixes inter-app model dependencies
from my_site import settings
import django.core.management
django.core.management.setup_environ(settings)  # mimic manage.py
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()  # validate the models - *THIS* is what was missing

#setup WSGI application object
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)