Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom action to Django inline object on the admin interface

I have an admin interface that has a blog post, with inline models which are previus versions of the post.

I'd like to add an action for each one of the previous version (A revert action, custom model method)

how should I go about doing that? its kinda similar to ModelAction actions keyword, but I want it to be inside the model view, not the list view and also its for each inline model, not for the parent model

would love some help.

to make it clearer

my previous_version class has a function named revert. all I want is that in my blog post's view in the admin panel by each previous version I'll have a link or button or something. and pressing it will call previous_version.revert.

like image 875
Omri Avatar asked Sep 24 '12 13:09

Omri


People also ask

How do you put a button on the list page for each row in Django admin page?

In the case of a button, you'll want to use format_html . This will mark the relevant parts of the string safe, and so that it's displayed as an HTML button, and escape the other parts to protect against various securit issues. The values of object.id and object. some_property will get inserted into the placeholders.

What we can do in admin portal in Django?

Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

What is admin interface in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


2 Answers

I guess the right thing to make this is Admin actions as described in documentation -

https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

like image 175
Janis Lankovskis Avatar answered Oct 02 '22 18:10

Janis Lankovskis


You can extend Blog ModelAdmin with action revert.

Overriding inline model template to add a button, like you said you already did is a good way to do it.

Just be sure to wrap created view within admin_view and allow only post requests.

like image 44
bmihelac Avatar answered Oct 02 '22 19:10

bmihelac