#6967 closed (fixed)
ModelForms doesn't validate CHOICES
Reported by: | Owned by: | Jacob | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | simon@…, carl@…, ville@…, pat.j.anderson@…, web@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A ModelForm with a CharField, that has CHOICES set, presents to the user only the specified CHOICES. But it does not _validate_ against the CHOICES, which allows a crafted response to write arbitrary values to the field.
Attachments (3)
Change History (23)
Changed 15 years ago by
Attachment: | choices-fix.patch added |
---|
comment:1 Changed 15 years ago by
comment:2 Changed 15 years ago by
Cc: | simon@… added |
---|
comment:3 Changed 15 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 Changed 15 years ago by
milestone: | → 1.0 |
---|
Changed 15 years ago by
Attachment: | 6967-tests.diff added |
---|
Some tests for choices on CharField and IntegerField.
comment:5 Changed 15 years ago by
Owner: | changed from nobody to Jacob |
---|---|
Status: | new → assigned |
comment:6 Changed 15 years ago by
comment:7 Changed 15 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:8 Changed 15 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
The test was insufficient, unfortunately. Field.formfield is going to need to cope with other model field types that set arbitrary arguments in their defaults dict which would make sense for the form field they expect to use, but won't make sense to TypedChoiceField. The only built-in examples of this are PositiveIntegerField and PositiveSmallIntegerField, which pass a min_value argument.
We could pop off min_value as max_length is being popped now, but it's just fixing the symptom. A custom model field could set anything.
comment:9 Changed 15 years ago by
I fell across this just now too. For now I just changed my local copy of the pop()
line to:
kwargs = kwargs.get('choices_kwargs', {})
Still not the nicest solution, but it isn't as bad as the current one.
comment:10 Changed 15 years ago by
Has patch: | set |
---|
comment:11 Changed 15 years ago by
Cc: | carl@… added |
---|
comment:13 Changed 15 years ago by
Cc: | ville@… added |
---|
comment:14 Changed 15 years ago by
Cc: | pat.j.anderson@… added |
---|
comment:15 Changed 15 years ago by
Cc: | web@… added |
---|
comment:16 Changed 15 years ago by
BTW, in the future please open a new ticket for things of this nature.
comment:17 Changed 15 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:18 Changed 14 years ago by
I'm getting this bug as well (running latest trunk as of two minutes ago). Here is my traceback:
Validating models... Unhandled exception in thread started by <function inner_run at 0x6aef70> Traceback (most recent call last): File "/Library/Python/2.5/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run self.validate(display_num_errors=True) File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 249, in validate num_errors = get_validation_errors(s, app) File "/Library/Python/2.5/site-packages/django/core/management/validation.py", line 28, in get_validation_errors for (app_name, error) in get_app_errors().items(): File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 131, in get_app_errors self._populate() File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 58, in _populate self.load_app(app_name, True) File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 74, in load_app models = import_module('.models', app_name) File "/Library/Python/2.5/site-packages/django/utils/importlib.py", line 35, in import_module __import__(name) File "/Users/stavros/Code/Django/head/main/models.py", line 215, in <module> class AdType(models.Model): File "/Users/stavros/Code/Django/head/main/models.py", line 221, in AdType price = models.DecimalField(_("price"), max_digits=6, decimal_places=2, min_value=0) File "/Library/Python/2.5/site-packages/django/db/models/fields/__init__.py", line 589, in __init__ Field.__init__(self, verbose_name, name, **kwargs) TypeError: __init__() got an unexpected keyword argument 'min_value'
I'm not sure what this means, but I can't launch my server.
comment:19 Changed 14 years ago by
I'm sorry, disregard my post above. I shouldn't post bug reports while sleepless...
Attached patch appears to fix this, but needs tests.