[Fixed]-How to alter/change the post data in django UpdateView

13👍

The QueryDict is mutable after you create its .copy(). See the docs.

Update Example:

class SomeUpdateView(UpdateView):

    def post(self, request, **kwargs):
        request.POST = request.POST.copy()
        request.POST['some_key'] = 'some_value'
        return super(SomeUpdateView, self).post(request, **kwargs)

Here is much broader discussion about the topic.

Furthermore, shouldn’t this be done in ModelForm subclass? You’re certainly aware you can set custom form as a form_class in UpdateView. Such a logic usually needs unit tests and it’s much easier to unit test logic which sits in the form.

Leave a comment