Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#2409 closed enhancement (wontfix)

Call model methods on object from admin interface

Reported by: Rudolph Owned by: adrian
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:

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.

Attachments (0)

Change History (7)

comment:1 Changed 8 years ago by ubernostrum

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 8 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

admin/<app>/<model>/change_list.html
and put your custom HTML in there.

comment:3 Changed 8 years ago by ubernostrum

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 8 years ago by adrian

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 8 years ago by Rudolph

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 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:7 Changed 7 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.