Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#2409 closed enhancement (wontfix)

Call model methods on object from admin interface

Reported by: Rudolph Froger Owned by: Adrian Holovaty
Component: contrib.admin Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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."""

    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 Changed 12 years ago by James Bennett

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.

comment:2 Changed 12 years ago by ian@…

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

and put your custom HTML in there.

comment:3 Changed 12 years ago by James Bennett

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 Changed 12 years ago by Adrian Holovaty

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 Changed 12 years ago by Rudolph Froger

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 Changed 12 years ago by Adrian Holovaty

Resolution: wontfix
Status: newclosed

Closing for the reasons I pointed out in the previous comment.

comment:7 Changed 11 years ago by (none)

milestone: Version 1.0

Milestone Version 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top