Django 1.4 的权限/身份验证文档提供了以下代码片段,用于以编程方式创建自定义权限:
编辑:(我想将其用于不一定与特定模型类相关联的权限,而是跨多种类型的更通用权限。)
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get(app_label='myapp', model='BlogPost')
permission = Permission.objects.create(codename='can_publish',
name='Can Publish Posts',
content_type=content_type)
我的问题是这段代码应该放在哪里。显然这些应该只创建一次,但我不想在 shell 中这样做。似乎这应该存储在某个文件中。(为了文档的缘故。)