Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#10363 closed (fixed)

ModelForm excluded field can still be updated

Reported by: jgoldberg Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:



class Object(models.Model):
    field_a = models.CharField(max_length=255)
    field_b = models.CharField(max_length=255)

class ObjectForm(ModelForm):
    field_a = forms.CharField(max_length=255)

    class Meta:
        model = Object
        exclude = ['field_a']

When you initialize ObjectForm with POST data that includes the *form* field_a, doing a save(commit=False) will return an Object with field_a containing the value from the POST data. This doesn't seem like expected behavior: since field_a is listed in the exclude list, the data should only go into cleaned_data['field_a'], and not the object. An easy fix, of course, is to not name your form-specific fields with the same names as your model fields. However, there are cases where I want to use ModelForm, but for a couple fields I want to do manual manipulation of the form data before I update my model object.

Attachments (2)

10363.diff (1.6 KB) - added by k0001 8 years ago.
fix and tests
exclude-save.diff (2.0 KB) - added by Alex Gaynor 8 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 8 years ago by Jacob

milestone: 1.1
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

Changed 8 years ago by k0001

Attachment: 10363.diff added

fix and tests

comment:2 Changed 8 years ago by k0001

Has patch: set

Changed 8 years ago by Alex Gaynor

Attachment: exclude-save.diff added

comment:3 Changed 8 years ago by Alex Gaynor

Triage Stage: AcceptedReady for checkin

comment:4 Changed 8 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [10575]) Fixed #10363 -- Modified ModelForm? handling to ensure that excluded fields can't be saved onto the model. Thanks to jgoldberg for the report, an Alex Gaynor for the fix.

comment:5 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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