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

Мак*_*вич 3 django django-forms django-admin

我有主要的 django 管理类,其中包括具有两个字段 pos_x 和 pos_y 的内联类。

我想通过 JS 和 modal-window 更新这个字段。

我可以在 InlineClass 中添加按钮而不更改任何管理 html 吗?

class InlineClass(CommonInlineConfigurationMixin):
  model = MapPoint
  fields = ['title', ('pos_x', 'pos_y')]
  form = Form
Run Code Online (Sandbox Code Playgroud)

ger*_*ett 7

我想你可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

并将 my_code.js 放入您的“static/js”文件夹中。