开发者

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

开发者 https://www.devze.com 2022-12-17 18:47 出处:网络
I\'m looking to add a m开发者_如何学Pythonethod 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 b

I'm looking to add a m开发者_如何学Pythonethod 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.


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.


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

0

精彩评论

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