Django

Code

Ticket #5913 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

Newforms ModelChoiceField and ModelMultipleChoiceField accepting invalid choices when cleaning

Reported by: cohcheto@gmail.com Assigned to: nobody
Milestone: Component: Forms
Version: SVN Keywords: newforms, clean
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

In this fields clean() method there is no validation if the returned object is actually in the valid choices if the queryset is filtered somehow

class ModelChoiceField(ModelChoiceField):
    ...
    def clean(self, value):
        Field.clean(self, value)
        if value in ('', None):
            return None
        try:
            value = self.queryset.model._default_manager.get(pk=value)
        except self.queryset.model.DoesNotExist:
            raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
        return value

Here we check only if there is an instance of that model with pk=value, but not if this object is a valid choice given the queryset Here is an example lets say we have a model TestModel? with attribute test_attribute which is BooleanField?:

>>> from django.newforms.models import ModelChoiceField
>>> from testapp.models import TestModel
>>> ins_1 = TestModel._default_manager.create(test_attribute=True)
>>> ins_2 = TestModel._default_manager.create(test_attribute=False)
>>> field = ModelChoiceField(queryset=TestModel._default_manager.filter(test_attribute=True))
>>> field.clean(ins_1._get_pk_value())
<TestModel: test_attribute - True> # or something like that
>>> field.clean(ins_2._get_pk_value())
<TestModel: test_attribute - False> # this is not a valid choice but it is cleaned like it is

I provide a patch its simple but probably not the best option its for ModelChoiceField? only but its the same for the multiple field

Attachments

model_choice_field.patch (0.6 kB) - added by cohcheto@gmail.com on 11/09/07 14:01:01.
Patch

Change History

11/09/07 14:01:01 changed by cohcheto@gmail.com

  • attachment model_choice_field.patch added.

Patch

11/09/07 14:08:08 changed by brosner

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to duplicate.
  • needs_tests changed.
  • needs_docs changed.

This falls under the same functionality that #4787 is wanting to give to ModelChoiceField and ModelMultipleChoiceField. Marking as duplicate. Perhaps you can write a better patch that includes this and attach it there?

11/10/07 09:51:38 changed by gwilson

  • status changed from closed to reopened.
  • resolution deleted.
  • stage changed from Unreviewed to Accepted.

Let's keep the issues separate please.

11/13/07 08:36:29 changed by gwilson

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6670]) Fixed #4787, #5913 -- Updating the queryset on a ModelChoiceField or ModelMultipleChoiceField now updates its widget's choices. The clean methods for ModelChoiceField and ModelMultipleChoiceField were changed to only allow choices in the specified queryset (instead of allowing all choices returned by the queryset model's default manager).


Add/Change #5913 (Newforms ModelChoiceField and ModelMultipleChoiceField accepting invalid choices when cleaning)




Change Properties
Action