allow uuid_format add more tests and update docs
This commit is contained in:
@ -318,6 +318,21 @@ class Decimal(ModelFieldFactory, decimal.Decimal):
|
||||
class UUID(ModelFieldFactory, uuid.UUID):
|
||||
_type = uuid.UUID
|
||||
|
||||
def __new__( # type: ignore # noqa CFQ002
|
||||
cls, *, uuid_format: str = "hex", **kwargs: Any
|
||||
) -> Type[BaseField]:
|
||||
kwargs = {
|
||||
**kwargs,
|
||||
**{
|
||||
k: v
|
||||
for k, v in locals().items()
|
||||
if k not in ["cls", "__class__", "kwargs"]
|
||||
},
|
||||
}
|
||||
|
||||
return super().__new__(cls, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def get_column_type(cls, **kwargs: Any) -> Any:
|
||||
return sqlalchemy_uuid.UUID()
|
||||
uuid_format = kwargs.get("uuid_format", "hex")
|
||||
return sqlalchemy_uuid.UUID(uuid_format=uuid_format)
|
||||
|
||||
@ -15,6 +15,10 @@ class UUID(TypeDecorator): # pragma nocover
|
||||
|
||||
impl = CHAR
|
||||
|
||||
def __init__(self, *args: Any, uuid_format: str = "hex", **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self.uuid_format = uuid_format
|
||||
|
||||
def _cast_to_uuid(self, value: Union[str, int, bytes]) -> uuid.UUID:
|
||||
if not isinstance(value, uuid.UUID):
|
||||
if isinstance(value, bytes):
|
||||
@ -28,7 +32,11 @@ class UUID(TypeDecorator): # pragma nocover
|
||||
return ret_value
|
||||
|
||||
def load_dialect_impl(self, dialect: DefaultDialect) -> Any:
|
||||
return dialect.type_descriptor(CHAR(36))
|
||||
return (
|
||||
dialect.type_descriptor(CHAR(36))
|
||||
if self.uuid_format == "string"
|
||||
else dialect.type_descriptor(CHAR(32))
|
||||
)
|
||||
|
||||
def process_bind_param(
|
||||
self, value: Union[str, int, bytes, uuid.UUID, None], dialect: DefaultDialect
|
||||
@ -37,7 +45,7 @@ class UUID(TypeDecorator): # pragma nocover
|
||||
return value
|
||||
if not isinstance(value, uuid.UUID):
|
||||
value = self._cast_to_uuid(value)
|
||||
return str(value)
|
||||
return str(value) if self.uuid_format == "string" else "%.32x" % value.int
|
||||
|
||||
def process_result_value(
|
||||
self, value: Optional[str], dialect: DefaultDialect
|
||||
|
||||
Reference in New Issue
Block a user