post_syncdb上不存在ContentType匹配查询 [英] ContentType matching query does not exist on post_syncdb
本文介绍了post_syncdb上不存在ContentType匹配查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加一些数据到数据库,一旦创建表,使用 post_syncdb
信号。
I am trying to add add some data to the database as soon as tables are created, using the post_syncdb
signal.
signals.post_syncdb.connect(init)
然后在init函数中,我想设置权限,所以我使用
Then in the init function, I want to set permission, so I use
ct = ContentType.objects.get(app_label='news', model='Article')
Permission(name='Approve articles', codename='can_approve_article', content_type=ct)
但是如果我删除所有表并运行 syncdb
,我得到
But if I drop all the tables and run syncdb
, I get
...
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次,只有前几次抛出错误。
- It works fine if I try this code outside
syncdb
. - It also works fine if I let
syncdb
create all the tables without this code, and then add this code and run syncdb without it having to make any changes. - AND I am pretty sure it used to work, but I changed a lot of things in other places since then, so I don't know where to start.
- I get the same error for other models in different apps.
- The signal is fired about 10 times, only the first few times throw the error.
非常感谢任何提示!
推荐答案
将其添加到init函数的开头:
Add this to the beginning of your init function:
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes() # make sure all content types exist
这篇关于post_syncdb上不存在ContentType匹配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文