orokusaki
orokusaki

Reputation: 57128

Django: Is there a way to add a model instance method to the admin?

I'm looking to add a method that my Ticket model has called process to the admin, so that I could click a link in the list view, and "process" my model instance (do an API call behind the scenes).

To clarify:

class Ticket(models.Model):
    title = models.CharField(max_length=255)

    def process(self):
        ... hardcore processing action ...

I need to add the process() method directly to the admin without using a separate function.

Upvotes: 0

Views: 433

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599628

You just need to provide a small method in your ModelAdmin class that returns a link pointing at a view that calls your model method, and add the name of that method to the modeladmin's list_display tuple. You'll obviously also need to define this view itself, and a url that points to it.

Upvotes: 4

fijter
fijter

Reputation: 18057

Yes, thats possible; Check out this documentation, just what you need:

http://docs.djangoproject.com/en/1.1/ref/contrib/admin/actions/#ref-contrib-admin-actions

Upvotes: 2

Related Questions