fix: unquote object_id when calling get_change_actions#137
fix: unquote object_id when calling get_change_actions#137nshafer wants to merge 1 commit intocrccheck:mainfrom
Conversation
|
I was unable to get a test case that recreated the original problem, I'll have to try it with the sample project next. Here's what I tried: diff --git a/django_object_actions/tests/test_admin.py b/django_object_actions/tests/test_admin.py
index db6e7da..472212c 100644
--- a/django_object_actions/tests/test_admin.py
+++ b/django_object_actions/tests/test_admin.py
@@ -47,7 +47,7 @@ class CommentTests(LoggedInTestCase):
class ExtraTests(LoggedInTestCase):
def test_action_on_a_model_with_complex_id(self):
- related_data = RelatedDataFactory()
+ related_data = RelatedDataFactory(id="t)e_st id€")
related_data_url = reverse(
"admin:polls_relateddata_change", args=(related_data.pk,)
)
@@ -56,6 +56,7 @@ class ExtraTests(LoggedInTestCase):
)
response = self.client.get(action_url)
+
self.assertNotEqual(response.status_code, 404)
self.assertRedirects(response, related_data_url) |
|
Sorry for lack of details, I was under a tight deadline at that time and just needed a fix ASAP. Anyways, I have created a new project that reproduces the error at https://github.com/nshafer/oatest. Steps to install and reproduce are in the README. What it really boils down to is that for an object with an ID such as "key_with_underscores", instead of that being passed to the |
I was recently overriding
get_change_actions()as instructed in the documentation, however I was having problems due to a model having a CharField for the primary key, and specifically values that included underscore characters. I was receiving what seemed to be invalid values inobject_idwhich extra characters.By default the django admin quotes primary key values via the
quote()function in "django/contrib/admin/utils.py". This is to prevent certain characters, such as underscores, in the primary key, especially if it's a CharField, from messing up the URL. Therefore, in the admin theobject_idis unquoted before callingget_object()and in other cases.Therefore, I think
object_idshould also be unquoted before callingget_change_actionsso thatobject_idis not url quoted any more, and operations such asobj = self.model.objects.get(pk=object_id)will succeed as expected.This PR simply does that. I tried for a short period to get tests running, but I've never installed poetry and was having issues, and just don't have time to debug it right now, so I wasn't able to write a test to confirm the behavior, apologies.