[1.1.X] 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. 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
This commit is contained in:
parent
5f70a72876
commit
08a698ed46
|
@ -148,7 +148,10 @@ class ForeignKeyRawIdWidget(forms.TextInput):
|
||||||
|
|
||||||
def label_for_value(self, value):
|
def label_for_value(self, value):
|
||||||
key = self.rel.get_related_field().name
|
key = self.rel.get_related_field().name
|
||||||
|
try:
|
||||||
obj = self.rel.to._default_manager.get(**{key: value})
|
obj = self.rel.to._default_manager.get(**{key: value})
|
||||||
|
except self.rel.to.DoesNotExist:
|
||||||
|
return ''
|
||||||
return ' <strong>%s</strong>' % escape(truncate_words(obj, 14))
|
return ' <strong>%s</strong>' % escape(truncate_words(obj, 14))
|
||||||
|
|
||||||
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
||||||
|
|
|
@ -130,3 +130,27 @@ class AdminForeignKeyWidgetChangeList(DjangoTestCase):
|
||||||
class OldAdminForeignKeyWidgetChangeList(AdminForeignKeyWidgetChangeList):
|
class OldAdminForeignKeyWidgetChangeList(AdminForeignKeyWidgetChangeList):
|
||||||
urls = 'regressiontests.admin_widgets.urls2'
|
urls = 'regressiontests.admin_widgets.urls2'
|
||||||
admin_root = '/deep/down/admin'
|
admin_root = '/deep/down/admin'
|
||||||
|
|
||||||
|
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.')
|
||||||
|
|
|
@ -19,8 +19,12 @@ class CarTireAdmin(admin.ModelAdmin):
|
||||||
return db_field.formfield(**kwargs)
|
return db_field.formfield(**kwargs)
|
||||||
return super(CarTireAdmin, self).formfield_for_foreignkey(db_field, request, **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 = WidgetAdmin(name='widget-admin')
|
||||||
|
|
||||||
site.register(models.User)
|
site.register(models.User)
|
||||||
site.register(models.Car, CarAdmin)
|
site.register(models.Car, CarAdmin)
|
||||||
site.register(models.CarTire, CarTireAdmin)
|
site.register(models.CarTire, CarTireAdmin)
|
||||||
|
site.register(models.Event, EventAdmin)
|
||||||
|
|
Loading…
Reference in New Issue