0

Well, let me explain this.

I am working on a simple django admin project.

In the admin.py file, I have the following admin classes:

class A_Admin(admin.ModelAdmin):
    #some stuff


class B_Admin(admin.ModelAdmin):
    #some stuff

I want to override the get_urls() method of A_Admin that if I click a button on A_Admin instance change page, it will redirect the page to B_Admin changelist page.

(I know there are many ways to do what I want and what I mentioned above is not the best, but this is what I want. So let skip the discussion why I insist on this solution.)

I want to the following:

def get_urls(self):
    #django's code
    #inside the urlpattern
    urlpattern = (
        #default urls from django admin
        .....
        url(r'^some_url$',
            wrap(super(B_Admin, self).changelist_view),
            name='%s_%s_delete' % info),
        ....)

    return urlpatterns

This is not working, since 'self' is a A_Admin class object rather than B_Admin obejct.

So is there any way to get the proxy of calss A_Admin inside B_Admin? I just wanna override changelist_view of A and call it inside B.

Is this possible?

Thanks in advance

4

1 回答 1

0

You should just instantiate B_Admin and use its method.

I believe the following code should work:

from django.contrib import admin
from my_app.models import B_Model  # The model for which B_Admin is used

def get_urls(self):
    #django's code
    #inside the urlpattern
    urlpattern = (
        #default urls from django admin
        .....
        url(r'^some_url$',
            wrap(B_Admin(B_Model, admin.site).changelist_view),
            name='%s_%s_delete' % info),
        ....)

return urlpatterns

UPDATE: Most probably, B_Admin was already instantiated when you called

admin.site.register(B_Model, B_Admin)

So instead of doing

B_Admin(B_Model, admin.site)

again you can just get it from the AdminSite's registry:

admin.site._registry[B_Model]
于 2013-05-14T17:20:10.363 回答