#13149: ForeignKeyRawIdWidget doesn't handle invalid values
-------------------------------------------+--------------------------------
          Reporter:  acdha                 |         Owner:  nobody       
            Status:  new                   |     Milestone:               
         Component:  django.contrib.admin  |       Version:  1.2          
        Resolution:                        |      Keywords:  raw_id_fields
             Stage:  Accepted              |     Has_patch:  1            
        Needs_docs:  0                     |   Needs_tests:  1            
Needs_better_patch:  1                     |  
-------------------------------------------+--------------------------------
Changes (by [email protected]):

  * needs_better_patch:  0 => 1
  * version:  1.1 => 1.2

Comment:

 Django 1.2+ needs to have patches in two files to get this issue fixed.

 changes to django/forms/models.py from line 984 (catch ValueError):

 {{{
     def to_python(self, value):
         if value in EMPTY_VALUES:
             return None
         try:
             key = self.to_field_name or 'pk'
             value = self.queryset.get(**{key: value})
         except (ValueError, self.queryset.model.DoesNotExist):
             raise ValidationError(self.error_messages['invalid_choice'])
         return value

 }}}

 And django/contrib/admin/widgets.py (catch ValueError as well):


 {{{
     def label_for_value(self, value):
         key = self.rel.get_related_field().name
         try:
             obj = self.rel.to._default_manager.using(self.db).get(**{key:
 value})
             return '&nbsp;<strong>%s</strong>' %
 escape(truncate_words(obj, 14))
         except (ValueError, self.rel.to.DoesNotExist):
             return ''

 }}}

 Applying both of these patches seems to fix the issue. It would be nice to
 have this fixed in the next Django 1.2.2 release.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13149#comment:4>
Django <http://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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to