@@ -32,21 +32,6 @@ def get_prep_value(self, value):
3232 def rel_db_type (self , connection ):
3333 return "objectId"
3434
35- def to_python (self , value ):
36- if value is None or isinstance (value , int ):
37- return value
38- try :
39- return ObjectId (value )
40- except (errors .InvalidId , TypeError ):
41- try :
42- return int (value )
43- except (ValueError , TypeError ):
44- raise exceptions .ValidationError (
45- self .error_messages ["invalid" ],
46- code = "invalid" ,
47- params = {"value" : value },
48- ) from None
49-
5035
5136class ObjectIdField (ObjectIdMixin , Field ):
5237 def get_internal_type (self ):
@@ -57,3 +42,15 @@ def deconstruct(self):
5742 if path .startswith ("django_mongodb.fields.objectid" ):
5843 path = path .replace ("django_mongodb.fields.objectid" , "django_mongodb.fields" )
5944 return name , path , args , kwargs
45+
46+ def to_python (self , value ):
47+ if value is None :
48+ return value
49+ try :
50+ return ObjectId (value )
51+ except (errors .InvalidId , TypeError ):
52+ raise exceptions .ValidationError (
53+ self .error_messages ["invalid" ],
54+ code = "invalid" ,
55+ params = {"value" : value },
56+ ) from None
0 commit comments