如何从 Django Admin 的 response_change 中获取自定义表单字段值?

How can I get custom form field value from within Django Admin's response_change?

我通过覆盖 change_form.html 向模型添加了自定义功能。基本上,如果这些更改得到管理员的批准,我允许用户更改模型的对象。我添加了两个按钮,分别命名为 accept-suggestiondecline-suggestion,我打算通过 response_change 方法处理自定义功能:

def response_change(self, request, obj):
    if "decline-suggestion" in request.POST:
        # do stuff...

    if "accept-suggestion" in request.POST:
        # do stuff...

这两个按钮都会向用户发送一封电子邮件,说明该建议是被拒绝还是被批准。到目前为止,一切都很好。问题是我想增加管理员的可能性,写一个简短的理由来解释为什么建议被拒绝。所以我又改了change_form.html

<div class="submit-row">
    <div class="float-left">
        <a class="decline-button-outlined accordion" type="button" href="#">DECLINE SUGGESTION</a>
    </div>
    <div class="float-right">
        <input class="accept-button" type="submit" name="accept-suggestion" value="ACEITAR SUGESTÃO">
    </div>
</div>

<div class="additional-infos">
    <fieldset class="module aligned">
        <div class="form-row">
            <label for="decline-reasons">Reasons for rejection:</label>
            <textarea
                placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
                id="decline-reasons" class="vLargeTextField" rows="5"></textarea>
        </div>
        <div class="submit-row">
            <div class="float-right">
                <input class="decline-button" type="submit" name="decline-suggestion" value="DECLINE">
            </div>
        </div>
    </fieldset>
</div>

这是最好的方法吗?如果是这样,如何从 response_change 中获取上面 <textarea> 的值?如果没有,您有什么建议?

非常感谢!

如果您将 name 添加到 <textarea>,您将能够在服务器端检索内容。没有 name,数据不会发送到服务器 (Django)。

所以像这样:

<textarea
    placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
    id="decline-reasons" name="decline-reasons" class="vLargeTextField" rows="5"></textarea>

应该允许您使用 request.POST["decline-reasons"] 在 Django 端检索文本。