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

Murilo Sitonio

I've added a custom functionality to a model by overriding change_form.html. Basically, I'm letting users change the objects of a model if these changes were approved by the admin. I added two buttons, named accept-suggestion and decline-suggestion and I intend to handle the custom functionality through response_change method:

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

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

Both buttons will send an e-mail to the user saying if the suggestion was declined or approaved. So far so good. The problem is that I want to add the possibility to the admin write a brief justification explaining why the suggestion was declined. So I changed change_form.html again.

<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>

Is this the best approach? If so, how can I get the value of the <textarea> above from within response_change? If not, what would you suggest?

Thank you very much!

Alex Watt

If you add a name to your <textarea> you will be able to retrieve the contents on the server side. Without a name, the data is not being sent to the server (Django).

So something like this:

<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>

Should allow you to retrieve the text on the Django side with request.POST["decline-reasons"].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I change a Django form field value before saving?

How can I set the field value in django admin based in another field's lookup?

how to get field value in django admin form save_model

How to properly add a custom field as a callable to Django admin change form?

Get to a Django field value from within the field

How can I get a field value by it's index within a result set in crystal reports?

How can I return the value of a custom form field

How can I call a value from a Custom Text Field and dynamically place it in Form Attribute?

How to get value from form field in django framework?

How can i get form->field() value?

How can i remove extra "s" from django admin panel?

Can I make an admin field not required in Django without creating a form?

Django - How can I change profile view in admin's panel?

In a user's Edit Info form, how do I include the name of the field(s) and user's input value(s) in a response from the model

How can I pass a value to Django Form's init method from a view?

How to Override or Hide Django admin model form field value

How can I access request object within a custom Serializer Field in the Django REST framework?

how remove field from admin view during add form django

How can I get a value from a JSON with a partially field name

How can I get a field value from a document?

How can I get data from a form model into a database in Django

how can i get the last modified row in a sheet to display a value in a form response from another sheet?

How do I add custom actions to a change model form in Django Admin?

How can I define a django model's field default value to be another field value?

How can I change display of visible value in django form?

How can I configure fields in a custom lightning record form as an admin?

How can I allow django admin to set a field to NULL?

How i can register on Django admin a field for upload of files

How can I include a ManyToMany field into django admin fieldsets?

TOP Ranking

HotTag

Archive