开发者

Is there an easy way to provide a custom django admin action without allowing changes to models?

I have a custom action on a model Foo all set up and ready to go, complete with a new permission I开发者_StackOverflow中文版've made.

Problem is, my administrators need the can_change_foo permission to view a change list and perform that custom action (which I don't want to award).

Is there an easier way to set this up without rewriting the model list admin view?


There is a horrid hack I can think of... Completely untested, obviously...

You could disable all other actions by overriding get_actions() and only allow for your custom action. Then you could follow T.Stone's suggestion here and completely disable links to edit individual instances of the model. What that would allow you to do is give your users the can_change_foo permission knowing that the only action they would be able to perform was yours.

Not pretty... Especially the part about not linking to the edit page...

Is re-writing the list admin view that bad? :-)


I ended up overriding the changelist_view() method on my ModelAdmin class, copying the defaults from django and just commenting out the permissions check. The list (at least the way I have it configured) doesn't have links to edit the individual objects, and even if it did, django raises a PermissionDenied if you try and edit an individual object. (since I never awarded the can_change permission).

It's a hack, and some monkeypatching, but until there's a separate permission for viewing a changelist, it works pretty well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜