Fixed #19951 -- Admin: Invalid model PK values shouldn't cause 500 status.
Avoid ValueError in admin when passing a string as PK for an inherited model with an integer PK field. Thanks ikks for the patch. See also #11191.
This commit is contained in:
parent
c31a9793c6
commit
e2ee02c5bd
|
@ -501,7 +501,7 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
try:
|
try:
|
||||||
object_id = model._meta.pk.to_python(object_id)
|
object_id = model._meta.pk.to_python(object_id)
|
||||||
return queryset.get(pk=object_id)
|
return queryset.get(pk=object_id)
|
||||||
except (model.DoesNotExist, ValidationError):
|
except (model.DoesNotExist, ValidationError, ValueError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_changelist_form(self, request, **kwargs):
|
def get_changelist_form(self, request, **kwargs):
|
||||||
|
|
|
@ -124,13 +124,22 @@ class AdminViewBasicTest(TestCase):
|
||||||
|
|
||||||
def testBasicEditGetStringPK(self):
|
def testBasicEditGetStringPK(self):
|
||||||
"""
|
"""
|
||||||
A smoke test to ensure GET on the change_view works (returns an HTTP
|
Ensure GET on the change_view works (returns an HTTP 404 error, see
|
||||||
404 error, see #11191) when passing a string as the PK argument for a
|
#11191) when passing a string as the PK argument for a model with an
|
||||||
model with an integer PK field.
|
integer PK field.
|
||||||
"""
|
"""
|
||||||
response = self.client.get('/test_admin/%s/admin_views/section/abc/' % self.urlbit)
|
response = self.client.get('/test_admin/%s/admin_views/section/abc/' % self.urlbit)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
|
def testBasicInheritanceGetStringPK(self):
|
||||||
|
"""
|
||||||
|
Ensure GET on the change_view works on inherited models (returns an
|
||||||
|
HTTP 404 error, see #19951) when passing a string as the PK argument
|
||||||
|
for a model with an integer PK field.
|
||||||
|
"""
|
||||||
|
response = self.client.get('/test_admin/%s/admin_views/supervillain/abc/' % self.urlbit)
|
||||||
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
def testBasicAddPost(self):
|
def testBasicAddPost(self):
|
||||||
"""
|
"""
|
||||||
A smoke test to ensure POST on add_view works.
|
A smoke test to ensure POST on add_view works.
|
||||||
|
|
Loading…
Reference in New Issue