gpt4 book ai didi

django - post_syncdb 上不存在 ContentType 匹配查询

转载 作者:行者123 更新时间:2023-12-01 10:05:13 26 4
gpt4 key购买 nike

我试图在创建表后立即添加一些数据到数据库中,使用 post_syncdb信号。

signals.post_syncdb.connect(init)

然后在init函数中,我想设置权限,所以我使用
ct = ContentType.objects.get(app_label='news', model='Article')
Permission(name='Approve articles', codename='can_approve_article', content_type=ct)

但是如果我删除所有表并运行 syncdb ,我得到
...
File "...\base\functions\init.py", line 11, in init
ct = ContentType.objects.get(app_label='news', model='Article')
...
django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.

我做过的一些测试:
  • 如果我在 syncdb 之外尝试此代码,它工作正常.
  • 如果我让 syncdb 也能正常工作创建没有此代码的所有表,然后添加此代码并运行syncdb而无需进行任何更改。
  • 而且我很确定它曾经有效,但从那以后我在其他地方改变了很多东西,所以我不知道从哪里开始。
  • 对于不同应用程序中的其他模型,我收到相同的错误。
  • 信号被触发大约 10 次,只有前几次抛出错误。

  • 非常感谢您的任何提示!

    最佳答案

    将此添加到您的 init 函数的开头:

     from django.contrib.contenttypes.management import update_all_contenttypes
    update_all_contenttypes() # make sure all content types exist

    关于django - post_syncdb 上不存在 ContentType 匹配查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11672976/

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