在Django应用启动时出现“str”没有属性"_default_manager"错误

4 投票
2 回答
6326 浏览
提问于 2025-04-16 16:21

在我重启Apache以应用新的Django更改后,大约会出现30秒到1分钟的以下错误:

ViewDoesNotExist: 尝试在模块 project.app.views 中找到 home_page。错误是:'str' 对象没有 '_default_manager' 属性

这些错误过一会儿就消失了,但这很奇怪。你知道怎么调试这个问题,或者可能是什么原因导致的吗?

2 个回答

1

我刚接触Django这个Python框架,我在view.py文件中的模型部分犯了个错误。

我写的是:model = 'Article'

ArticleListViews(ListView):
    model = 'Article'
    template_name = ‘article.html'

结果还是报了同样的错。其实正确的写法是model = Article,我不需要把模型的名字放在引号里。

祝你编码愉快!

9

我觉得这是个bug:

http://code.djangoproject.com/ticket/10405#comment:11

这个问题看起来很符合你的情况,因为在谷歌搜索中找不到其他相关信息,而且你的问题过一段时间就消失了——根据这个问题单,是因为模型字符串的懒加载导致的。

评论中建议在你的管理自动发现功能之前添加以下内容。

from django.db.models.loading import cache as model_cache
if not model_cache.loaded:
    model_cache.get_models()

撰写回答