简体   繁体   English

如何在 Inline Django Admin 字段上添加自定义按钮?

[英]How can I add custom button on Inline Django Admin fields?

I have main django admin class that includes inline class with two fields pos_x and pos_y.我有主要的 django 管理类,其中包括具有两个字段 pos_x 和 pos_y 的内联类。

I want to update this fields via JS and modal-window.我想通过 JS 和 modal-window 更新这个字段。

Can I add button in InlineClass without changing any admin html?我可以在不更改任何管理 html 的情况下在 InlineClass 中添加按钮吗?

class InlineClass(CommonInlineConfigurationMixin):
  model = MapPoint
  fields = ['title', ('pos_x', 'pos_y')]
  form = Form

I guess you can do it like this:我想你可以这样做:

class InlineClass(CommonInlineConfigurationMixin):
    model = MapPoint
    fields = ['title', ('pos_x', 'pos_y'), 'button']
    readonly_fields = ("button",)
    form = Form
    class Media:
        js = ("js/my_code.js",)

    def button (self, obj):
      return "<button type='button' onclick='my_code()'>Click Me!</button>"

    button.allow_tags=True

And put the my_code.js into your "static/js" folder.并将 my_code.js 放入您的“static/js”文件夹中。

Using ger.s.brett answer i have to format html in return, like this:使用ger.s.brett答案我必须格式化 html 作为回报,如下所示:

def button (self, obj):
   return format_html("<button type='button' onclick='my_code()'>Click Me!</button>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM