Forms have list-mutability issues with field validator lists
|Reported by:||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.