[英]How to override and call super for response_change or response_add in django admin
[英]How can I get custom form field value from within Django Admin's response_change?
我通過覆蓋change_form.html
向模型添加了自定義功能。 基本上,如果管理員批准了這些更改,我會讓用戶更改模型的對象。 我添加了兩個按鈕,命名為accept-suggestion
和decline-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>
的值? 如果沒有,你有什么建議?
非常感謝!
如果您向<textarea>
添加name
,您將能夠在服務器端檢索內容。 沒有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 端的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.