How can I retrieve the ModelAdmin given the model class or an instance?

13 views
Skip to first unread message

Marco Rogers

unread,
Jan 6, 2010, 5:55:37 PM1/6/10
to Django users
I'm retrieving a model using django.db.models.get_model. The view is
receiving a post from a form generated by the ModelAdmin. How can I
get a hold of that ModelAdmin instance so can then get a hold of the
ModelForm? This will allow me to process the post (validation,
related objects, etc). The ModelAdmin has a reference to it's model,
but the reverse doesn't seem to be true.

The way I'm doing this right now is bad juju. The AdminSite keeps a
registry of models and ModelAdmins so I'm doing this:

modeladmin = admin.site._registry[model];

Obviously using this internal registry is a bad idea. I was looking
for a more official api similar to get_model or one of the following:

modeladmin = admin.site.get_admin(model)

Even if this just returns the instance from the registry like above.
Or make it accessible through the model like one of the following:

modeladmin = model.get_admin()
modeladmin = model._admin

Any help with this is appreciated

Now a litlte more context. Like I mentioned above, what I actually
need is a ModelForm similar to the one generated by the admin site. I
have a custom admin view that receives the post from an add/change
form. So to process that post I need the equivalent ModelForm.

If there is an easier or more idiomatic way to write custom admin
views, I'd appreciate if someone pointed me in the right direction.

Reply all
Reply to author
Forward
0 new messages