对于对象级权限,我使用django-guardian。如何将其集成到自己的代码中的文档很好:http://packages.python.org/django-guardian/userguide/admin-integration.html
但我如何才能将其添加到其他应用程序的模型中呢?我不想修改例如django.contrib.auth的代码。
发布于 2012-04-19 22:09:45
我在django-reversion的源代码中找到了一个解决方案。有一个名为patch_admin()的帮助器。这是为django-guardian修改的代码片段。
# Copy of django-reversion helpers.py
def patch_admin(model, admin_site=None):
"""
Enables version control with full admin integration for a model that has
already been registered with the django admin site.
This is excellent for adding version control to existing Django contrib
applications.
"""
admin_site = admin_site or admin.site
try:
ModelAdmin = admin_site._registry[model].__class__
except KeyError:
raise NotRegistered, "The model %r has not been registered with the admin site." % model
# Unregister existing admin class.
admin_site.unregister(model)
# Register patched admin class.
class PatchedModelAdmin(GuardedModelAdmin, VersionAdmin, ModelAdmin): # Remove VersionAdmin, if you don't use reversion.
pass
admin_site.register(model, PatchedModelAdmin)
from django.contrib.auth.models import Group
patch_admin(Group)https://stackoverflow.com/questions/10227787
复制相似问题