Adding an extra button to a single object in django admin

I hope they didn’t ask for it, and I just missed it, but I was looking for a bunch and could not find anything.

I am adding an extra save button for the django admin when adding or modifying an object. This is pretty easy to do. I simply overloaded submit_line.html to add an additional button, and then overrided the save_model function to check the name of that button. It works great.

My problem is that I need this button for only one object ... not all of them. I looked in the file change_form.html to find out how it knows which object it is dealing with, and found {{opts.module_name}}, but it doesn't seem to be available in submit_line.html. I tried to print it, and nothing appeared.

I also thought about hacking save_as (not very elegant, but I don't really care about this particular project), but this button only appears when changing .. not on adding, so this will not work.

Does anyone know how to determine which object I am working with in submit_line.html? Or any other way to do this?

Thank!

+3
source share
2 answers

You can do this using javascript as follows:

/static/js/useful.js

$ (document) .ready (function ($) {
    $ ('input [name = "_ addanother"]'). before ('<input type = "submit" name = "_ use" value = "Useful functionality" />');
});

and in your ModelAdmin add:

class MyModelAdmin (admin.ModelAdmin):
     class Media:
        js = ('/static/js/useful.js',)
+4

change_view original. , {{ original.id }} !

0

Source: https://habr.com/ru/post/1768262/


All Articles