Opened 14 years ago
Closed 14 years ago
#17127 closed Bug (fixed)
Validators list is shared among fields of different form instances
| Reported by: | Claude Paroz | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When forms are created, fields are not new instances, but instead are a deepcopy of the Form class base_fields. If you modified afterwards a form field validators list, it is modified for all other form instances. Example:
from django import forms
from django.core import validators
class MyForm(forms.Form):
myfield = forms.CharField(max_length=25)
f1 = MyForm()
f2 = MyForm()
f1.fields['myfield'].validators
>>> [<django.core.validators.MaxLengthValidator object at 0x32b53d0>]
f2.fields['myfield'].validators
>>> [<django.core.validators.MaxLengthValidator object at 0x32b53d0>]
f1.fields['myfield'].validators[0] = validators.MaxValueValidator(12)
f1.fields['myfield'].validators
>>> [<django.core.validators.MaxValueValidator object at 0x3205a90>]
f2.fields['myfield'].validators
>>> [<django.core.validators.MaxValueValidator object at 0x3205a90>]
This is a problem for me for example to properly resolve #17125.
Attachments (1)
Change History (4)
by , 14 years ago
| Attachment: | 17127-1.diff added |
|---|
comment:1 by , 14 years ago
| Has patch: | set |
|---|
comment:2 by , 14 years ago
| Triage Stage: | Unreviewed → Ready for checkin |
|---|
Yes, this is a bug. Changing validators on a specific form's field instance should not change it for all instances. Patch looks good to me.
Note:
See TracTickets
for help on using tickets.
Isolate validators list between form instances