Opened 11 years ago

Closed 11 years ago

#19668 closed New feature (wontfix)

Form enhancement: `Form.set_data` to set data and files

Reported by: Byron Ruth Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

This simply moves the logic for setting is_bound, data and files into a method which enables setting the data after form initialization. This is a backwards compatible. This makes it easier to not have to initialize a form twice in view. It changes from this idiom:

def view(request):
    if request.method == 'POST':
        form = Form(request.POST, request.FILES)
        ...
    else:
        form = Form()
    ...

to simply this:

def view(request):
    form = Form()
    if request.method == 'POST':
        form.set_data(request.POST, request.FILES)
    ...

This reduces clutter and redundancy when there are a lot of form arguments passed.

Change History (6)

comment:2 by Byron Ruth, 11 years ago

Summary: Form enhancement: `Form.set_data` to set data an filesForm enhancement: `Form.set_data` to set data and files

comment:3 by anonymous, 11 years ago

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedDesign decision needed

I'm -0 on this change, but i'm not a core dev.

The argument against this would be that much of a form's functionality changes based on data, fields, valid/invalid, cleaned data, errors etc.. Changing the data at the wrong time could cause a lot of problems. Changing set_data to raise an error if the form is already bound may be enough to address some of those concerns. I'd suggest raising the issue on django-developers to get some opinions from core devs.

comment:4 by anonymous coward, 11 years ago

-1

You don't save a single line of code (unless you copy the provided example, which doesn't complete the antithesis version), are adding another method to maintain, and are creating the potential for internal problems (as mentioned above), not to mention making Form less extensible.

comment:5 by Rafal Stozek, 11 years ago

-1, and you can make it even shorter:

form = Form(request.POST or None, request.FILES or None)
if request.method == 'POST':
    # code

comment:6 by Claude Paroz, 11 years ago

Resolution: wontfix
Status: newclosed

This proposal received mostly negative opinions until now (also on django-developers). I also think that this would be an important change in the contract that Form data are set at initialization time, for few benefits but a high risk of triggering bugs in the form stack.
Anyway, thanks for taking the time to make the proposal.

Note: See TracTickets for help on using tickets.
Back to Top