Skip to content

Commit 6abb648

Browse files
timgrahamWaVEV
authored andcommitted
add MongoAutoField support for numeric pks in model form data
1 parent 936671c commit 6abb648

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

django_mongodb/fields/auto.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ def get_prep_value(self, value):
2626
try:
2727
return ObjectId(value)
2828
except errors.InvalidId as e:
29+
# A manually assigned integer ID?
30+
if isinstance(value, str) and value.isdigit():
31+
return int(value)
2932
raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e
3033

3134
def rel_db_type(self, connection):
3235
return Field().db_type(connection=connection)
3336

3437
def to_python(self, value):
35-
if value is None:
38+
if value is None or isinstance(value, int):
3639
return value
3740
try:
3841
return ObjectId(value)

0 commit comments

Comments
 (0)