Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#4271 closed (wontfix)

Form should use a copy of data passed to it

Reported by: Gary Wilson <gary.wilson@…> Owned by: Adrian Holovaty
Component: Forms Version: dev
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Usually, one passes POST data directly to the form like so:

if request.method == 'POST':
    form = MyForm(request.POST)

and request.POST is an immutable QueryDict. This means that you cannot fiddle with the data, for say if you wanted to remove keys so that the data doesn't get redisplayed when the form has errors.

I could make the copy in my own code:

form = MyForm(request.POST.copy())

but just thought this might be something useful to do more generally.

If anyone has a better alternative for not redisplaying submitted data, please mention it.

Attachments (1)

4271.diff (564 bytes ) - added by Gary Wilson <gary.wilson@…> 17 years ago.
use copy of passed data.

Download all attachments as: .zip

Change History (4)

by Gary Wilson <gary.wilson@…>, 17 years ago

Attachment: 4271.diff added

use copy of passed data.

comment:1 by Gary Wilson <gary.wilson@…>, 17 years ago

Has patch: set
Triage Stage: UnreviewedDesign decision needed

comment:2 by Malcolm Tredinnick, 17 years ago

Resolution: wontfix
Status: newclosed

Based on the the django-dev thread, this didn't get a lot of support from core developers. The logic was that
the copy operation is not free, so in the odd case when the developer wants to modify the data, they can make the copy manually (since it's not difficult).

comment:3 by Gary Wilson <gary.wilson@…>, 17 years ago

For those needing this, here would be a way to put it in your form so that the developer using your form doesn't need to know to pass a copy of the post data in the view:

class MyForm(forms.Form):
    def __init__(self, data=None, *args, **kwargs):
        if data:
            data = data.copy()
        super(MyForm, self).__init__(data, *args, **kwargs)
Note: See TracTickets for help on using tickets.
Back to Top