Opened 13 years ago

Closed 13 years ago

#17603 closed Bug (duplicate)

Forms have list-mutability issues with field validator lists

Reported by: brett@… Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

from django.forms import Form
from django.forms.fields import IntegerField
from django.core.validators import MinValueValidator

class SampleForm(Form):
    some_field = IntegerField()

    def __init__(self, *args, **kwargs):
        super(SampleForm, self).__init__(*args, **kwargs)
        self.fields['some_field'].validators.append(MinValueValidator(1))

first_form = SampleForm()
print len(first_form.fields['some_field'].validators)
# Prints 1

second_form = SampleForm()
print len(second_form.fields['some_field'].validators)
# Prints 2

I believe this has to do with the fact that the deepcopy method on Field , which does a shallow copy of everything except for 'widget'. I could see two potential solutions here:

  • Do a deepcopy on everything (potentially risky)
  • Do a deepcopy on validators and any other fields which could potentially be a list.

While searching, I found ticket #4167, which was similar to this, but pertained more to the Field class itself having mutability issues rather than the Form initialization code.

Change History (1)

comment:1 by Claude Paroz, 13 years ago

Resolution: duplicate
Status: newclosed

This has been fixed in #17127 (will be in 1.4)

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