我尝试了同样的事情.. 用一个假模型来持有额外的权限.. 我还有其他问题.. 和你的不一样.. 但是问题.. 所以我决定不顾一切地写一些代码来显式地创建我的额外权限,然后我将该代码放入我的库中:
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def get_or_create_permission(codename, name, content_type):
permission = Permission.objects.filter(content_type=content_type, codename=codename).first()
if not permission:
permission = Permission.objects.create(content_type=content_type, codename=codename, name=name)
print "Created Permission: %s in content type: %s" % (permission, content_type,)
else:
print "Found Permission: %s in ContentType: %s" % (permission, content_type,)
return permission
def get_or_create_content_type(app_label, name, model):
content_type = ContentType.objects.filter(app_label=app_label, model=model).first()
if not content_type:
content_type = ContentType.objects.create(app_label=app_label, model=model, name=name)
print "Created ContentType: %s" % content_type
else:
print "Found ContentType: %s" % content_type
return content_type
现在在你的 urls.py 中(或其他任何将在启动时执行的地方):
content_type = get_or_create_content_type('appname', 'Feeds', 'feeds')
get_or_create_permission('change_feed_defaults', 'Change Feed Defaults', content_type)
get_or_create_permission('view_logs', 'View Logs', content_type)
# etc
这可确保您的权限在启动时就在那里。这些天我会为此做一个包装器,这样我只需要传递一个权限字典,它就会在一个函数调用中完成所有事情......