Repository: wagtail/wagtail

Issue:
Primary keys are cast instead of relying on serialization
The javascript multi views for dashboard choosers are inconsistent with handling of primary keys.  In some places they cast to int and others they rely on serialization.

For example, the [images.views.multiple.add](https://github.com/wagtail/wagtail/blob/master/wagtail/images/views/multiple.py#L81) view uses both approaches in the same view.  It would be best to let Django handle serialization to avoid edge cases.

Similar to https://github.com/wagtail/wagtail/issues/6509 - I am happy to fix these today if you approve the issue.  Looks like there are two spots where this happens - document and image multiple add view.

[There is already a field in the tests that can be used for testing on the custom image and custom document tests](https://github.com/wagtail/wagtail/blob/master/wagtail/tests/customuser/fields.py#L11)


Discussion:

Resolving patch:
diff --git a/wagtail/admin/views/generic/multiple_upload.py b/wagtail/admin/views/generic/multiple_upload.py
--- a/wagtail/admin/views/generic/multiple_upload.py
+++ b/wagtail/admin/views/generic/multiple_upload.py
@@ -54,13 +54,13 @@ def get_edit_object_form_context_data(self):
         edit_form_class = self.get_edit_form_class()
         return {
             self.context_object_name: self.object,
-            "edit_action": reverse(self.edit_object_url_name, args=(self.object.id,)),
+            "edit_action": reverse(self.edit_object_url_name, args=(self.object.pk,)),
             "delete_action": reverse(
-                self.delete_object_url_name, args=(self.object.id,)
+                self.delete_object_url_name, args=(self.object.pk,)
             ),
             "form": edit_form_class(
                 instance=self.object,
-                prefix="%s-%d" % (self.edit_object_form_prefix, self.object.id),
+                prefix="%s-%d" % (self.edit_object_form_prefix, self.object.pk),
                 user=self.request.user,
             ),
         }
@@ -71,7 +71,7 @@ def get_edit_object_response_data(self):
         """
         return {
             "success": True,
-            self.context_object_id_name: int(self.object.id),
+            self.context_object_id_name: self.object.pk,
             "form": render_to_string(
                 self.edit_form_template_name,
                 self.get_edit_object_form_context_data(),
@@ -213,7 +213,7 @@ def post(self, request, *args, **kwargs):
         self.model = self.get_model()
         self.form_class = self.get_edit_form_class()
 
-        self.object = get_object_or_404(self.model, id=object_id)
+        self.object = get_object_or_404(self.model, pk=object_id)
 
         if not self.permission_policy.user_has_permission_for_instance(
             request.user, "change", self.object
@@ -234,14 +234,14 @@ def post(self, request, *args, **kwargs):
             return JsonResponse(
                 {
                     "success": True,
-                    self.context_object_id_name: int(object_id),
+                    self.context_object_id_name: self.object.pk,
                 }
             )
         else:
             return JsonResponse(
                 {
                     "success": False,
-                    self.context_object_id_name: int(object_id),
+                    self.context_object_id_name: self.object.pk,
                     "form": render_to_string(
                         self.edit_form_template_name,
                         {
@@ -271,7 +271,10 @@ class DeleteView(View):
     def post(self, request, *args, **kwargs):
         object_id = kwargs[self.pk_url_kwarg]
         self.model = self.get_model()
-        self.object = get_object_or_404(self.model, id=object_id)
+        self.object = get_object_or_404(self.model, pk=object_id)
+        object_id = (
+            self.object.pk
+        )  # retrieve object id cast to the appropriate type (usually int)
 
         if not self.permission_policy.user_has_permission_for_instance(
             request.user, "delete", self.object
@@ -283,7 +286,7 @@ def post(self, request, *args, **kwargs):
         return JsonResponse(
             {
                 "success": True,
-                self.context_object_id_name: int(object_id),
+                self.context_object_id_name: object_id,
             }
         )
 


Test patch: