We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3e817db commit 2a59bdcCopy full SHA for 2a59bdc
django_mongodb/fields/objectid.py
@@ -1,4 +1,4 @@
1
-from bson import ObjectId
+from bson import ObjectId, errors
2
from bson.errors import InvalidId
3
from django.core import exceptions
4
from django.db.models.fields import Field
@@ -34,6 +34,14 @@ def to_python(self, value):
34
params={"value": value},
35
) from None
36
37
+ def get_prep_value(self, value):
38
+ if value is None:
39
+ return None
40
+ try:
41
+ return ObjectId(value)
42
+ except (errors.InvalidId, TypeError) as e:
43
+ raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e
44
+
45
def deconstruct(self):
46
name, path, args, kwargs = super().deconstruct()
47
if path.startswith("django_mongodb.fields.objectid"):
0 commit comments