Mar*_*ark 3 django signals syncdb contenttype
我正在尝试使用post_syncdb信号创建表时立即向数据库添加一些数据.
signals.post_syncdb.connect(init)
Run Code Online (Sandbox Code Playgroud)
然后在init函数中,我想设置权限,所以我使用
ct = ContentType.objects.get(app_label='news', model='Article')
Permission(name='Approve articles', codename='can_approve_article', content_type=ct)
Run Code Online (Sandbox Code Playgroud)
但是,如果我放下所有的桌子并运行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.
Run Code Online (Sandbox Code Playgroud)
我做过的一些测试:
syncdb.syncdb在没有此代码的情况下创建所有表,它也可以正常工作,然后添加此代码并运行syncdb而无需进行任何更改.非常感谢任何提示!
小智 7
将其添加到init函数的开头:
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes() # make sure all content types exist
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |