Skip to content

Commit 2c0928a

Browse files
committed
support numeric pks in model form data
1 parent 457a889 commit 2c0928a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

django_mongodb/fields/auto.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def get_prep_value(self, value):
2323
# like settings.SITE_ID which has a system check requiring an integer.
2424
if isinstance(value, (ObjectId | int)):
2525
return value
26+
# If a digit doesn't match the length of an ObjectId, treat it as an
27+
# integer. Manually assigned IDs won't be 24 digits long.
28+
if isinstance(value, str) and value.isdigit() and len(value) != 24:
29+
return int(value)
2630
try:
2731
return ObjectId(value)
2832
except errors.InvalidId as e:
@@ -32,7 +36,7 @@ def rel_db_type(self, connection):
3236
return Field().db_type(connection=connection)
3337

3438
def to_python(self, value):
35-
if value is None:
39+
if value is None or isinstance(value, int):
3640
return value
3741
try:
3842
return ObjectId(value)

0 commit comments

Comments
 (0)