Skip to content

Commit 6c7689a

Browse files
committed
fix: patch BaseAPIViewSet URL patterns to accept ObjectId hex PKs
get_urlpatterns uses <int:pk> generating [0-9]+ patterns that reject ObjectId values. get_object_detail_urlpath passes pk directly to reverse() which fails for ObjectId args. Patch both: use re_path with [0-9a-fA-F]{24}|\d+ and str()-convert pk before reversing.
1 parent 6cddf25 commit 6c7689a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

  • src/dbx_python_cli/templates/project_template/project_name/settings/apps

src/dbx_python_cli/templates/project_template/project_name/settings/apps/wagtail.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ def _translation_of_filter(self, request, queryset, view):
173173
except ImportError:
174174
pass
175175

176+
# Patch BaseAPIViewSet.get_urlpatterns to use a regex that accepts
177+
# both 24-char ObjectId hex strings and plain integers for <pk>, and
178+
# patch get_object_detail_urlpath to str()-convert pk before reverse()
179+
# so ObjectId values produce valid URLs.
180+
try:
181+
from django.urls import path as _dj_path
182+
from django.urls import re_path as _dj_re_path
183+
from django.urls import reverse as _dj_reverse
184+
from wagtail.api.v2.views import BaseAPIViewSet
185+
186+
@classmethod
187+
def _api_get_urlpatterns(cls):
188+
return [
189+
_dj_path("", cls.as_view({"get": "listing_view"}), name="listing"),
190+
_dj_re_path(
191+
r"^(?P<pk>[0-9a-fA-F]{24}|\d+)/$",
192+
cls.as_view({"get": "detail_view"}),
193+
name="detail",
194+
),
195+
_dj_path("find/", cls.as_view({"get": "find_view"}), name="find"),
196+
]
197+
198+
@classmethod
199+
def _api_get_object_detail_urlpath(cls, model, pk, namespace=""):
200+
url_name = (namespace + ":detail") if namespace else "detail"
201+
return _dj_reverse(url_name, args=(str(pk),))
202+
203+
BaseAPIViewSet.get_urlpatterns = _api_get_urlpatterns
204+
BaseAPIViewSet.get_object_detail_urlpath = _api_get_object_detail_urlpath
205+
except ImportError:
206+
pass
207+
176208
# Patch ModelViewSet.pk_path_converter so viewsets whose model uses
177209
# ObjectIdAutoField get "object_id" URL converter instead of "int".
178210
# ObjectIdAutoField inherits AutoField → IntegerField, so Wagtail's

0 commit comments

Comments
 (0)