Add a hack to handle exclude fields while dumping model fields to dictionary.
The hack should be removed after a pydantic bug is fixed: https://github.com/pydantic/pydantic/issues/6575 Also, use SerializationInfo to get mode of serialization and convert ObjectId fields to string only if json response is required with mode=json in model_dump function. That is due to other operations such as
inserting or updating objects in DB doesn't need the conversion as data types should be ObjectId in the DB.
Add a hack to handle
exclude
fields while dumping model fields to dictionary. The hack should be removed after a pydantic bug is fixed: https://github.com/pydantic/pydantic/issues/6575 Also, useSerializationInfo
to get mode of serialization and convertObjectId
fields to string only ifjson
response is required withmode=json
inmodel_dump
function. That is due to other operations such as inserting or updating objects in DB doesn't need the conversion as data types should beObjectId
in the DB.