Fixed #13149 -- The admin `ForeignKeyRawIdWidget` now properly handles non-integer values. Thanks, Chris Adams.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13751 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2010-09-11 18:34:40 +00:00
parent fffe0a00a3
commit 9e04c3b744
3 changed files with 16 additions and 4 deletions

View File

@ -154,9 +154,9 @@ class ForeignKeyRawIdWidget(forms.TextInput):
key = self.rel.get_related_field().name key = self.rel.get_related_field().name
try: try:
obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
except self.rel.to.DoesNotExist:
return ''
return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14)) return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
except (ValueError, self.rel.to.DoesNotExist):
return ''
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
""" """
@ -169,7 +169,7 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
attrs['class'] = 'vManyToManyRawIdAdminField' attrs['class'] = 'vManyToManyRawIdAdminField'
if value: if value:
value = ','.join([str(v) for v in value]) value = ','.join([force_unicode(v) for v in value])
else: else:
value = '' value = ''
return super(ManyToManyRawIdWidget, self).render(name, value, attrs) return super(ManyToManyRawIdWidget, self).render(name, value, attrs)

View File

@ -1006,7 +1006,7 @@ class ModelChoiceField(ChoiceField):
try: try:
key = self.to_field_name or 'pk' key = self.to_field_name or 'pk'
value = self.queryset.get(**{key: value}) value = self.queryset.get(**{key: value})
except self.queryset.model.DoesNotExist: except (ValueError, self.queryset.model.DoesNotExist):
raise ValidationError(self.error_messages['invalid_choice']) raise ValidationError(self.error_messages['invalid_choice'])
return value return value

View File

@ -1,3 +1,5 @@
# encoding: utf-8
from django import forms from django import forms
from django.contrib import admin from django.contrib import admin
from django.contrib.admin import widgets from django.contrib.admin import widgets
@ -151,3 +153,13 @@ class AdminForeignKeyRawIdWidget(DjangoTestCase):
post_data) post_data)
self.assertContains(response, self.assertContains(response,
'Select a valid choice. That choice is not one of the available choices.') 'Select a valid choice. That choice is not one of the available choices.')
def test_invalid_target_id(self):
for test_str in ('Iñtërnâtiônàlizætiøn', "1234'", -1234):
# This should result in an error message, not a server exception.
response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
{"band": test_str})
self.assertContains(response,
'Select a valid choice. That choice is not one of the available choices.')