#11716: Various methods in django.db.models.fields don't wrap ValueErrors and 
allow
them to escape
-------------------------------------+-------------------------------------
     Reporter:  Leo                  |                    Owner:
                                     |  albertoconnor
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  1
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by albertoconnor):

 I have been able to verify that the issue isn't reproduced in master
 locally using the following models.py and form.py

 {{{
 # models.py
 from django.db import models


 class Bar(models.Model):
     name = models.TextField()


 class Foo(models.Model):
     bar = models.ForeignKey(Bar, blank=True, null=True)
 }}}

 {{{
 # forms.py
 from django import forms

 from .models import Foo, Bar


 class FooForm(forms.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]

     class Meta:
         model = Foo
         fields = ['bar']
 }}}

 If I run the following
 {{{
 >>> form = FooForm(dict(bar='---'))
 >>> form.is_valid()
 False
 >>> form.errors
 {'bar': [u'Select a valid choice. That choice is not one of the available
 choices.']}
 }}}

 If I pass in something else like 'f' for bar it fails in the same way. The
 interesting thing about '---' is it is actually a choice which was
 inserted in the choices option for the field.

 I believe the next step to write a test which tests this case so we can
 make sure there isn't a regression. The test will also test this case
 through CI. There seem to be tests for the IntegerField case already so I
 will write a test for the ModelChoiceField case first thing tomorrow
 morning.

--
Ticket URL: <https://code.djangoproject.com/ticket/11716#comment:15>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.11d938310cce5f20b9d427d820897205%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to