- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下错误似乎在我的实时服务器上随机发生(即通过 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'
软件包和版本
数据库: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
最佳答案
对于仍然发现这个老问题的人的快速说明:这种情况也可能是由使用无效字符串作为引用的ForeignKey/ManyToMany/OnetoOne引起的(例如:未正确指向模型)。
我正在更新/重构一个项目并遇到了这个问题。原来这只是一个错字。
有点奇怪的 django 没有明确通知它无法解析字符串,可能是因为其他应用程序混淆了它。
关于Django属性错误: 'str' object has no attribute '_default_manager' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12458309/
我正在使用 Django Rest Framework 来创建一些 api。我正在使用工厂男孩来创建测试实例。我有一个名为 base_model 的抽象模型,该模型由项目的所有其他模型继承。
我正在使用 Django Rest Framework 来创建一些 api。我正在使用工厂男孩来创建测试实例。我有一个名为 base_model 的抽象模型,该模型由项目的所有其他模型继承。
我正在尝试将干草堆搜索添加到我的 base.html 中,以将其全局包含在我的网站上。但当我提交搜索时出现错误 我得到: Django-Haystack: 'NoneType' object has
以下错误似乎在我的实时服务器上随机发生(即通过 apache mod_wsgi),但从未在开发中发生(即 localhost python manage.py runserver)。 请注意,这种情况
按照实用 Django 教程出现此错误。在这里和谷歌上搜索过,没有发现任何提及这一点的内容,所以我想这将是我很容易错过的事情。异常(exception)情况在 gambino_tags.py 中: c
在我重新启动 Apache 以获取新的 Django 更改后,我在大约 30 秒到 1 分钟后收到以下错误: ViewDoesNotExist:已尝试模块 project.app.views 中的 h
我有以下问题: 当我尝试通过模板从我的数据库中删除成员时,我收到以下错误: 类型对象 'Member' 没有属性 '_default_manager 我不确定我做错了什么,因为一切似乎都尽可能正常。
在本地使用 django celery beat 我收到错误“PeriodicTask”对象没有属性“_default_manager”。我正在使用 Django 1.10。当我安排一项任务时,它会起
我正在尝试使用生菜来实现 BDD 以创建一个休息框架 api 但是当我执行命令 python manage.py harvest 我得到以下错误 /rest_framework/serializers
我是一名优秀的程序员,十分优秀!