#2409 closed enhancement (wontfix)
Call model methods on object from admin interface
Reported by: | Rudolph Froger | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It would be really nice if you could add buttons to the admin interface of an object. A click on the button calls the corresponding model method:
class Subscriber(models.Model): name = models.CharField('name', maxlength=40) def send_invoice(self): """Sends invoice by e-mail to subscriber.""" pass class Admin: actions = (('send_invoice', _('Send invoice')),)
This would create a button with the text 'Send invoice' in the admin interface of the object. The "actions" syntax should probably also allow passing arguments to the method.
Change History (7)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
i'm not going to get into what belongs into the framework debate.
just going to mention you can do what you want by defining a custom HTML template for you listing page
in your template directory create
admin/<app>/<model>/change_list.html
and put your custom HTML in there.
comment:3 by , 18 years ago
Well, sooner or later we (the Django developer community) are going to have to sit down and have a serious discussion about what does and doesn't go in the admin app, because people inevitably want to have all sorts of stuff added in to it and it'd be nice to have some set of guidelines to refer to.
Now's as good a time as any, so I'll draft up some ideas and throw them to django-dev.
comment:4 by , 18 years ago
I must say, I kinda like this one.
A bunch of questions, though: What happens if the method raises an exception, and where does the page redirect to after the method has been called?
And how do permissions come into play? Does a person need "Change" permissions to click that custom button? "Add" permissions? Specific, per-button special-case permissions?
Hmmm. Maybe this is verging on "too complex," on second thought. Still, I'd like to see answers to these questions before making a final judgment.
comment:5 by , 18 years ago
Adrian, good point about the permissions. Maybe each method automatically gets its own permission, like in the example 'Can send invoice'.
When someone clicks the button it just executes the method and returns to the same view. Maybe the user should get some kind of feedback, like a dialogwindow: "Send invoice executed" so the user knows the button did it's job. The feedback could be a message the method returns.
The syntax could be extended to allow to give default arguments to methods:
class Admin: actions = (('send_invoice', {'verbose_name': _('Send invoice'), 'args': {'including_vat': True}}),)
I think the method should not raise exceptions except for unrecoverable errors. The whole admin interface works like that, I think.
Which view should contain these buttons? List or object view?
comment:6 by , 18 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Closing for the reasons I pointed out in the previous comment.
I'm not sure that's really in scope for the admin application; it feels much more like the kind of thing you'd want to develop a specialized interface for.