开发者

Adding an extra button to one object in django admin

I hope this hasn't been asked and I just missed it, but I searched a bunch and couldn't find anything.

I'm adding an extra save button to the django admin when adding or changing an object. Doing that is fairly easy. I just overrode the submit_line.html to add the extra button and then overrode the save_model function to check for the name of that button. It works great.

My problem is that I only need this button to appear for one particular object... not all of them. I looked in change_form.html to see how it knows what object it is dealing with and found {{ opts.module_name }}, but it doesn't seem to be accessible in submit_line.html. I tried printing it out and nothing showed up.

I also thought about hacking save_as (not very graceful开发者_如何学编程, but I don't really care for this particular project), but that button only shows up on change.. not on add, so that won't work.

Does anyone know how to detect what object I'm working with in submit_line.html? Or any other way of doing this?

Thanks!


You can do it using javascript like this:

/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',)


You should be able to access the original object in change_view's context through original. For example {{ original.id }} should print its id!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜