You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the page about Custom field types we can see the following phrase at the start:
Sometimes, it might be required to customize as well the field BSON serialization. In order to do this, the field class will have to implement the __bson__ class method.
However the it does show this example that, as we can see, doesn't contains any method called __bson__, instead it uses the WithBsonSerializer annotation:
fromtypingimportAnnotatedfromodmanticimportAIOEngine, Model, WithBsonSerializerclassASCIISerializedAsBinaryBase(str):
@classmethoddef__get_validators__(cls):
yieldcls.validate@classmethoddefvalidate(cls, v):
ifisinstance(v, bytes): # Handle data coming from MongoDBreturnv.decode("ascii")
ifnotisinstance(v, str):
raiseTypeError("string required")
ifnotv.isascii():
raiseValueError("Only ascii characters are allowed")
returnvdefserialize_ascii_to_bytes(v: ASCIISerializedAsBinaryBase) ->bytes:
# We can encode this string as ascii since it contains# only ascii charactersbytes_=v.encode("ascii")
returnbytes_ASCIISerializedAsBinary=Annotated[
ASCIISerializedAsBinaryBase, WithBsonSerializer(serialize_ascii_to_bytes)
]
classExample(Model):
field: ASCIISerializedAsBinaryengine=AIOEngine()
awaitengine.save(Example(field="hello world"))
fetched=awaitengine.find_one(Example)
print(fetched.field)
#> hello world
Bug
In the page about Custom field types we can see the following phrase at the start:
However the it does show this example that, as we can see, doesn't contains any method called
__bson__
, instead it uses theWithBsonSerializer
annotation:Current Behavior
Go to https://art049.github.io/odmantic/fields/#custom-field-types and read the doc.
Expected behavior
The technique described in the doc and the one used in the example should match.
Environment
python -c "import pydantic.utils; print(pydantic.utils.version_info())
): N/AThe text was updated successfully, but these errors were encountered: