From 2890c2295bbd9144e30a74e7be9d11b4903a5101 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Tue, 2 Mar 2010 02:28:49 +0000 Subject: [PATCH] Fixed #11465: Ensure nonexistent pks enterd in an admin raw id field do not cause a server error. Thanks for report and initial patch sacre@wp.pl. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12648 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/widgets.py | 5 +++- tests/regressiontests/admin_widgets/tests.py | 24 +++++++++++++++++++ .../admin_widgets/widgetadmin.py | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 0954d3f3e2..d7befe4688 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -152,7 +152,10 @@ class ForeignKeyRawIdWidget(forms.TextInput): def label_for_value(self, value): key = self.rel.get_related_field().name - obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) + try: + obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) + except self.rel.to.DoesNotExist: + return '' return ' %s' % escape(truncate_words(obj, 14)) class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py index 4113e82c1f..fd0c25ca1a 100644 --- a/tests/regressiontests/admin_widgets/tests.py +++ b/tests/regressiontests/admin_widgets/tests.py @@ -127,3 +127,27 @@ class AdminForeignKeyWidgetChangeList(DjangoTestCase): def test_changelist_foreignkey(self): response = self.client.get('%s/admin_widgets/car/' % self.admin_root) self.failUnless('%s/auth/user/add/' % self.admin_root in response.content) + +class AdminForeignKeyRawIdWidget(DjangoTestCase): + fixtures = ["admin-widgets-users.xml"] + admin_root = '/widget_admin' + + def setUp(self): + self.client.login(username="super", password="secret") + + def tearDown(self): + self.client.logout() + + def test_nonexistent_target_id(self): + band = models.Band.objects.create(name='Bogey Blues') + pk = band.pk + band.delete() + post_data = { + "band": u'%s' % pk, + } + # Try posting with a non-existent pk in a raw id field: this + # should result in an error message, not a server exception. + response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root, + post_data) + self.assertContains(response, + 'Select a valid choice. That choice is not one of the available choices.') diff --git a/tests/regressiontests/admin_widgets/widgetadmin.py b/tests/regressiontests/admin_widgets/widgetadmin.py index 9257c306c9..6f15d9208b 100644 --- a/tests/regressiontests/admin_widgets/widgetadmin.py +++ b/tests/regressiontests/admin_widgets/widgetadmin.py @@ -19,8 +19,12 @@ class CarTireAdmin(admin.ModelAdmin): return db_field.formfield(**kwargs) return super(CarTireAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) +class EventAdmin(admin.ModelAdmin): + raw_id_fields = ['band'] + site = WidgetAdmin(name='widget-admin') site.register(models.User) site.register(models.Car, CarAdmin) site.register(models.CarTire, CarTireAdmin) +site.register(models.Event, EventAdmin)