以下错误似乎在我的实时服务器上随机发生(即通过 apache mod_wsgi),但从未在开发中发生(即 localhost python manage.py runserver)。
请注意,这种情况很少发生,并且不是可以轻松复制或每次访问特定网址时都可以复制的内容。
我已经在 SO 和 google 上看到了各种答案,但似乎没有任何明确的原因说明为什么会发生此错误。也许这是因为错误相当普遍,但最常见的答案似乎是由于循环导入错误。我看到的另一个答案是模型 FK 字段引用不是正确的情况(例如 applabel.model 而不是 applabel.Model),但我所有的模型 FK 字段都是正确的。
错误的原因似乎指向我的 admin.py 文件之一。该文件最初确实从 forms.py 文件中导入了自定义表单类。admin.py 文件和 forms.py 文件都从 models.py 文件中导入了相同的模型。因此,我将表单类移动到 admin.py 文件中,以防此处发生循环引用,但我仍然偶尔会遇到这些错误。
谁能解释为什么会发生此错误以及为什么会如此随机?我始终确保在代码更新后重新启动相关服务。
追溯是:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 101, in get_response
request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 250, in resolve
for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 279, in _get_url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 274, in _get_urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/myproject/urls.py", line 6, in <module>
admin.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
import_module('%s.admin' % app)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/myproject/myapps/app/admin.py", line 61, in <module>
class CardAdminForm(forms.ModelForm):
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 205, in __new__
opts.exclude, opts.widgets, formfield_callback)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 159, in fields_for_model
formfield = f.formfield(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 913, in formfield
'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to),
AttributeError: 'str' object has no attribute '_default_manager'
包和版本
- 警告:找不到 pymssql==2.0.0b1-dev-20111019 的 svn 位置
- 警告:找不到分发的 svn 位置==0.6.24dev-r0
- Django==1.3.3
- GnuPGInterface==0.3.2
- 景观客户端==12.05
- PAM==0.4.2
- 太平==1.1.7
- 扭曲核心==11.1.0
- apt-xapian-index==0.44
- argparse==1.2.1
- chardet==2.0.1
- 命令未找到==0.2.44
- ## FIXME:在此包的依赖链接中找不到 svn URL:distribute==0.6.24dev-r0
- django-debug-toolbar==0.9.4
- django-rosetta==0.6.8
- httplib2==0.7.2
- iotop==0.4.4
- 钥匙圈==0.7.1
- 语言选择器==0.1
- 启动板库==1.9.12
- lazr.restfulclient==0.12.0
- lazr.uri==1.0.3
- 水银==2.0.2
- oauth==1.0.1
- psycopg2==2.4.5
- pyOpenSSL==0.12
- pycrypto==2.4.1
- ## FIXME:在此包的依赖链接中找不到 svn URL:pymssql==2.0.0b1-dev-20111019
- pyserial==2.5
- python-apt==0.8.3ubuntu7
- python-debian==0.1.21ubuntu1
- 报告实验室==2.5
- simplejson==2.3.2
- ufw==0.31.1-1
- wadlib==1.3.0
- wsgiref==0.1.2
- xlwt==0.7.4
- zope.interface==3.6.1
数据库:PostgreSQL 9.1.5
CardAdmin 和 CardAdminForm:
class CardAdmin(admin.ModelAdmin):
form = CardAdminForm
raw_id_fields = ('cust', 'acc', 'vehicle', 'driver')
list_display = ('id', 'pan', 'name', 'expiry', 'created', 'modified')
list_filter = ('status', )
search_fields = ['id', 'pan']
admin.site.register(Card, CardAdmin)
class CardAdminForm(forms.ModelForm):
"""
A Form for Cards (Admin console)
"""
def __init__(self, *args, **kwargs):
super(CardAdminForm, self).__init__(*args, **kwargs)
self.fields['cust'].required = True
self.fields['acc'].required = True
self.fields['name'].required = True
self.fields['code'].widget = forms.PasswordInput()
self.fields['code'].max_length = 6
class Meta:
model = Card
fields = (
'cust',
'name',
'acc',
'no',
'code',
'type',
'status',
'address_1',
'address_2',
'zip',
'city',
'country',
'phone_no',
'expiry',
'vehicle',
'driver'
)
def save(self, commit=True):
# Save some additional data.
form_instance = super(CardAdminForm, self).save(commit=False)
cleaned_data = self.cleaned_data
form_instance.pan = '%s%s%s'\
% (
cleaned_data['acc'].iso.number,
cleaned_data['acc'].number,
cleaned_data['no']
)
if commit:
form_instance.save()
return form_instance