2012-04-19 91 views

回答

1

我在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) 
相關問題