Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 2890c2295bbd9144e30a74e7be9d11b4903a5101 1 parent 17c466a
Karen Tracey authored March 02, 2010
5  django/contrib/admin/widgets.py
@@ -152,7 +152,10 @@ def url_parameters(self):
152 152
 
153 153
     def label_for_value(self, value):
154 154
         key = self.rel.get_related_field().name
155  
-        obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
  155
+        try:
  156
+            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
  157
+        except self.rel.to.DoesNotExist:
  158
+            return ''
156 159
         return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
157 160
 
158 161
 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
24  tests/regressiontests/admin_widgets/tests.py
@@ -127,3 +127,27 @@ def tearDown(self):
127 127
     def test_changelist_foreignkey(self):
128 128
         response = self.client.get('%s/admin_widgets/car/' % self.admin_root)
129 129
         self.failUnless('%s/auth/user/add/' % self.admin_root in response.content)
  130
+
  131
+class AdminForeignKeyRawIdWidget(DjangoTestCase):
  132
+    fixtures = ["admin-widgets-users.xml"]
  133
+    admin_root = '/widget_admin'
  134
+
  135
+    def setUp(self):
  136
+        self.client.login(username="super", password="secret")
  137
+
  138
+    def tearDown(self):
  139
+        self.client.logout()
  140
+
  141
+    def test_nonexistent_target_id(self):
  142
+        band = models.Band.objects.create(name='Bogey Blues')
  143
+        pk = band.pk
  144
+        band.delete()
  145
+        post_data = {
  146
+            "band": u'%s' % pk,
  147
+        }
  148
+        # Try posting with a non-existent pk in a raw id field: this
  149
+        # should result in an error message, not a server exception.
  150
+        response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
  151
+            post_data)
  152
+        self.assertContains(response,
  153
+            'Select a valid choice. That choice is not one of the available choices.')
4  tests/regressiontests/admin_widgets/widgetadmin.py
@@ -19,8 +19,12 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
19 19
             return db_field.formfield(**kwargs)
20 20
         return super(CarTireAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
21 21
 
  22
+class EventAdmin(admin.ModelAdmin):
  23
+    raw_id_fields = ['band']
  24
+
22 25
 site = WidgetAdmin(name='widget-admin')
23 26
 
24 27
 site.register(models.User)
25 28
 site.register(models.Car, CarAdmin)
26 29
 site.register(models.CarTire, CarTireAdmin)
  30
+site.register(models.Event, EventAdmin)

0 notes on commit 2890c22

Please sign in to comment.
Something went wrong with that request. Please try again.