Skip to content

Commit

Permalink
Fix UUID representation
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Nov 20, 2023
1 parent 498f849 commit 57c03b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions mongoz/core/connection/collections.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from bson.binary import UuidRepresentation
from motor.motor_asyncio import AsyncIOMotorCollection


Expand All @@ -9,3 +10,7 @@ class Collection:
def __init__(self, name: str, collection: AsyncIOMotorCollection) -> None:
self._collection: AsyncIOMotorCollection = collection
self.name = name
self.set_uuid_representation(UuidRepresentation.STANDARD)

def set_uuid_representation(self, representation: int) -> None:
self._collection.codec_options.uuid_representation = representation
2 changes: 2 additions & 0 deletions mongoz/core/db/documents/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
no_type_check,
)

from bson import UuidRepresentation
from pydantic._internal._model_construction import ModelMetaclass

from mongoz.core.connection.collections import Collection
Expand Down Expand Up @@ -319,6 +320,7 @@ def __search_for_fields(base: Type, attrs: Any) -> None:

new_class.__db_document__ = True
meta.collection = meta.database.get_collection(collection_name)
meta.collection._collection.codec_options.uuid_representation = UuidRepresentation.STANDARD

mongoz_fields: Dict[str, MongozField] = {}
for field_name, field in new_class.model_fields.items():
Expand Down
1 change: 0 additions & 1 deletion mongoz/core/db/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ def __new__(cls, **kwargs: Any) -> BaseField: # type: ignore
**kwargs,
**{k: v for k, v in locals().items() if k not in CLASS_DEFAULTS},
}

return super().__new__(cls, **kwargs)


Expand Down

0 comments on commit 57c03b3

Please sign in to comment.