Skip to content

Commit 7b4eb61

Browse files
committed
fix: map ObjectIdAutoField to CharField in Wagtail API serializer
ObjectIdAutoField inherits AutoField, so DRF's ClassLookupDict maps it to IntegerField, which raises TypeError: int() argument ... not 'ObjectId'. Add ObjectIdAutoField → CharField to BaseSerializer.serializer_field_mapping so page IDs are serialized as 24-char hex strings instead.
1 parent 6c7689a commit 7b4eb61

1 file changed

Lines changed: 13 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ def _translation_of_filter(self, request, queryset, view):
173173
except ImportError:
174174
pass
175175

176+
# Patch Wagtail API serializer_field_mapping so ObjectIdAutoField PKs
177+
# are serialized as strings (hex) rather than integers.
178+
# ObjectIdAutoField inherits AutoField → DRF maps it to IntegerField,
179+
# which raises TypeError when trying int(ObjectId(...)).
180+
try:
181+
from django_mongodb_backend.fields import ObjectIdAutoField
182+
from rest_framework.fields import CharField
183+
from wagtail.api.v2.serializers import BaseSerializer
184+
185+
BaseSerializer.serializer_field_mapping[ObjectIdAutoField] = CharField
186+
except ImportError:
187+
pass
188+
176189
# Patch BaseAPIViewSet.get_urlpatterns to use a regex that accepts
177190
# both 24-char ObjectId hex strings and plain integers for <pk>, and
178191
# patch get_object_detail_urlpath to str()-convert pk before reverse()

0 commit comments

Comments
 (0)