Changing the field in the Django form after submit

duQue200 report abuse

Hi! I want to change the value of the form field in Django after the user submits the form. But when I try to do this in the view, I am getting the error that the request.POST object is not mutable. Is it possible to do what I want?

Answers

ChioL report abuse

Yes, you can't change the field of the request.POST object. I don't know why Django developers made this object immutable. But I don't also know why do you want to change this field. Maybe the final result could be achieved using another way?

duQue200 report abuse

I want to make some computations using other fields' values. Namely, I need to take the text entered in another field, send it to external API, then update the special field of the submitted form using the response from API.

ChioL report abuse

And what is further? What is the purpose of the form?

duQue200 report abuse

This is the model form. So, I need to create a new record in the database table.

ChioL report abuse

Then you probably can create an object representing the record in the table, but don't commit it. Something like:

new_obj = my_form.save(commit=False)

Then update the attribute of the new_obj:

new_obj.target_attribute = 'new_value_from_api'

And then save the updated object into the database:

new_obj.save()

This should work.

duQue200 report abuse

Thank you @ChioL very much! It is exactly what I wanted to do!

Add Answer

Need support?

Just drop us an email to ... Show more