Skip to content

Commit b736603

Browse files
committed
fix: patch ModelViewSet.pk_path_converter for MongoDB ObjectId PKs
ObjectIdAutoField inherits from AutoField → IntegerField, so Wagtail's pk_path_converter returns "int" for all MongoDB models, generating [0-9]+ URL patterns that reject ObjectId hex strings. Replace the cached_property descriptor with a regular property that checks for ObjectIdAutoField before the IntegerField fallback.
1 parent 71d64a1 commit b736603

1 file changed

Lines changed: 24 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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ def build_node(self, obj, context):
5757
except ImportError:
5858
pass
5959

60+
# Patch ModelViewSet.pk_path_converter so viewsets whose model uses
61+
# ObjectIdAutoField get "object_id" URL converter instead of "int".
62+
# ObjectIdAutoField inherits AutoField → IntegerField, so Wagtail's
63+
# isinstance(pk, IntegerField) check incorrectly picks "int".
64+
try:
65+
from django.db import models
66+
from django_mongodb_backend.fields import ObjectIdAutoField
67+
from wagtail.admin.viewsets.model import ModelViewSet
68+
69+
@property
70+
def _pk_path_converter(self):
71+
if isinstance(self.model_opts.pk, ObjectIdAutoField):
72+
return "object_id"
73+
if isinstance(self.model_opts.pk, models.UUIDField):
74+
return "uuid"
75+
if isinstance(self.model_opts.pk, models.IntegerField):
76+
return "int"
77+
return "str"
78+
79+
ModelViewSet.pk_path_converter = _pk_path_converter
80+
del _pk_path_converter
81+
except ImportError:
82+
pass
83+
6084

6185
class CustomWagtailDocsConfig(WagtailDocsAppConfig):
6286
@property

0 commit comments

Comments
 (0)