Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11465: Ensure nonexistent pks enterd in an admin raw i…

…d field do not

cause a server error. Thanks for report and initial patch sacre@wp.pl. 

Backport of r12648 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12649 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 08a698ed462b0e20a3a15b196152fee53e48ced9 1 parent 5f70a72
Karen Tracey authored March 02, 2010
5  django/contrib/admin/widgets.py
@@ -148,7 +148,10 @@ def url_parameters(self):
148 148
 
149 149
     def label_for_value(self, value):
150 150
         key = self.rel.get_related_field().name
151  
-        obj = self.rel.to._default_manager.get(**{key: value})
  151
+        try:
  152
+            obj = self.rel.to._default_manager.get(**{key: value})
  153
+        except self.rel.to.DoesNotExist:
  154
+            return ''
152 155
         return '&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))
153 156
 
154 157
 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
24  tests/regressiontests/admin_widgets/tests.py
@@ -130,3 +130,27 @@ def test_changelist_foreignkey(self):
130 130
 class OldAdminForeignKeyWidgetChangeList(AdminForeignKeyWidgetChangeList):
131 131
     urls = 'regressiontests.admin_widgets.urls2'
132 132
     admin_root = '/deep/down/admin'
  133
+
  134
+class AdminForeignKeyRawIdWidget(DjangoTestCase):
  135
+    fixtures = ["admin-widgets-users.xml"]
  136
+    admin_root = '/widget_admin'
  137
+
  138
+    def setUp(self):
  139
+        self.client.login(username="super", password="secret")
  140
+
  141
+    def tearDown(self):
  142
+        self.client.logout()
  143
+
  144
+    def test_nonexistent_target_id(self):
  145
+        band = models.Band.objects.create(name='Bogey Blues')
  146
+        pk = band.pk
  147
+        band.delete()
  148
+        post_data = {
  149
+            "band": u'%s' % pk,
  150
+        }
  151
+        # Try posting with a non-existent pk in a raw id field: this
  152
+        # should result in an error message, not a server exception.
  153
+        response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
  154
+            post_data)
  155
+        self.assertContains(response,
  156
+            '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 08a698e

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