Django 中模型创建的顺序
我在 django 中创建了一个权限系统。它位于一个名为 HelperApp
的应用程序中。首先通过post_syncdb
信号将权限定义输入数据库(发送者是HelperApp的models
,不包含任何模型类),然后监听django.contrib.auth 中的
,并为其添加默认权限。User
的post_save
现在的问题是,当还没有数据库文件并且我调用manage.pysyncdb
时,Auth应用程序将在创建User
表后立即创建一些默认用户(超级用户和匿名用户)。权限系统尝试赋予他们默认权限,但权限尚未创建。
目前,只有当我在 settings.py
INSTALLED_APPS
中将“HelperApp”放在“django.contrib.auth”之前,它才能工作。问题是,有没有一种方法可以定义表创建的顺序,而无需在 INSTALLED_APPS 中调整名称?
I have created a permission system in django. It is inside an App called HelperApp
. First it inputs permissions definition into the database with post_syncdb
signals(sender is the models
of HelperApp, which does not contain any model classes), and then listens to the post_save
of User
in django.contrib.auth
, and add default permissions to them.
Now the problem is, when there was no database file yet and I call manage.py syncdb
, the Auth App will create some default users right after the User
table is created (Superuser and AnonymousUser). The permission system tries to give them default permissions, but the permissions are not created yet.
Currently, only if I put 'HelperApp' before 'django.contrib.auth' in settings.py
INSTALLED_APPS
will it work. The question is, is there a way to define the sequence of table creation without the need of juggling names in INSTALLED_APPS
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用固定装置来创建初始数据: https://docs.djangoproject.com /en/dev/howto/initial-data/
Just use fixtures to create your inital data: https://docs.djangoproject.com/en/dev/howto/initial-data/