﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
11716	Various methods in django.db.models.fields don't wrap ValueErrors and allow them to escape	Leo Shklovskii	albertoconnor	"Passing in a '---' as the value for a ModelChoiceField causes an exception down in query.py that filters up through the form.

Here is specifically what I'm doing:
{{{
class Bar(Model):
    name = TextField()

class Foo(Model):
    bar = ForeignKey(Bar, blank=True, null=True)

class FooForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)

        bars = list(Bar.objects.order_by('name'))
        self.fields['bar'].choices = [('---','---')] + [(b.id, b.name) for b in bars]
}}}

If someone submits the form without selecting a Bar in the dropdown, the view does the standard `is_valid()` call and blows up.

But even if setting the field up this way that isn't a good idea (which it may not be), a client can submit whatever they want to the view that processes the form so this is a situation where submitting an invalid value creates a 500 rather than being caught in the field and reported as a form error.

Here is the full traceback
{{{
  File ""C:\ws\fuzzy\EvoworxSite\src\testapp\views.py"", line 13, in do_test
    if form.is_valid():

  File ""C:\Python25\lib\site-packages\django\forms\forms.py"", line 120, in is_valid
    return self.is_bound and not bool(self.errors)

  File ""C:\Python25\lib\site-packages\django\forms\forms.py"", line 111, in _get_errors
    self.full_clean()

  File ""C:\Python25\lib\site-packages\django\forms\forms.py"", line 240, in full_clean
    value = field.clean(value)

  File ""C:\Python25\lib\site-packages\django\forms\models.py"", line 993, in clean
    value = self.queryset.get(**{key: value})

  File ""C:\Python25\lib\site-packages\django\db\models\query.py"", line 299, in get
    clone = self.filter(*args, **kwargs)

  File ""C:\Python25\lib\site-packages\django\db\models\query.py"", line 498, in filter
    return self._filter_or_exclude(False, *args, **kwargs)

  File ""C:\Python25\lib\site-packages\django\db\models\query.py"", line 516, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))

  File ""C:\Python25\lib\site-packages\django\db\models\sql\query.py"", line 1675, in add_q
    can_reuse=used_aliases)

  File ""C:\Python25\lib\site-packages\django\db\models\sql\query.py"", line 1614, in add_filter
    connector)

  File ""C:\Python25\lib\site-packages\django\db\models\sql\where.py"", line 56, in add
    obj, params = obj.process(lookup_type, value)

  File ""C:\Python25\lib\site-packages\django\db\models\sql\where.py"", line 269, in process
    params = self.field.get_db_prep_lookup(lookup_type, value)

  File ""C:\Python25\lib\site-packages\django\db\models\fields\__init__.py"", line 210, in get_db_prep_lookup
    return [self.get_db_prep_value(value)]

  File ""C:\Python25\lib\site-packages\django\db\models\fields\__init__.py"", line 361, in get_db_prep_value
    return int(value)

ValueError: invalid literal for int() with base 10: '---'
}}}

Digging into the code it looks like `django.db.models.fields.AutoField.get_db_prep_value` should look something like this:
{{{
    def get_db_prep_value(self, value):
        if value is None:
            return value
        try:
            return int(value)
        except (TypeError, ValueError):
            raise exceptions.ValidationError(
                _(""This value must be an integer.""))
}}}

Which is just like the `to_python()` method above it.

Further, it looks like `IntegerField` and several others in that file have the same problem and let the ValueErrors escape."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		Dan Fairs albertoconnor	Accepted	1	1	0	0	0	0
