Skip to content

Commit 12f3722

Browse files
committed
add MongoAutoField support for numeric pks in model form data
1 parent a697d62 commit 12f3722

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6969
"aggregation_regress.tests.AggregationTests.test_more_more_more3",
7070
# Incorrect JOIN with GenericRelation gives incorrect results.
7171
"aggregation_regress.tests.AggregationTests.test_aggregation_with_generic_reverse_relation",
72-
# MongoAutoField.get_prep_value() must accept numeric pks.
73-
"model_forms.tests.ModelFormBasicTests.test_int_pks",
7472
}
7573
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
7674
_django_test_expected_failures_bitwise = {

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)