Django:有没有办法向管理添加模型实例方法?
我希望向管理员添加一个我的票证模型已调用 process 的方法,以便我可以单击列表视图中的链接,并“处理”我的模型实例(在幕后执行 API 调用)。
澄清一下:
class Ticket(models.Model):
title = models.CharField(max_length=255)
def process(self):
... hardcore processing action ...
我需要将 process() 方法直接添加到管理中,而不使用单独的函数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在 ModelAdmin 类中提供一个小方法,该方法返回一个指向调用模型方法的视图的链接,并将该方法的名称添加到 modeladmin 的
list_display
元组中。显然,您还需要定义此视图本身以及指向它的 url。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.是的,那是可能的;查看此文档,正是您所需要的:
http://docs.djangoproject.com/en/1.1/ref/contrib/admin/actions/#ref-contrib-admin-actions
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