[docs]classUUIDField(UUIDFieldBase):""" UUID Field This field can store uuid value, but with the option to add binary compression. If used as a primary key, it will auto-generate a UUID4 by default. ``binary_compression`` (bool): If True, the UUID will be stored in binary format. This will save 6 bytes per UUID in the database. Note: that this is a MySQL-only feature. See https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support/ for more details. """SQL_TYPE="CHAR(36)"def__init__(self,binary_compression:bool=True,**kwargs:Any)->None:if(kwargs.get("primary_key")orkwargs.get("pk",False))and"default"notinkwargs:kwargs["default"]=uuid4super().__init__(**kwargs)ifbinary_compression:self.SQL_TYPE="BINARY(16)"self._binary_compression=binary_compression
[docs]defto_db_value(self,value:Any,instance:"Union[Type[Model], Model]")->Optional[Union[str,bytes]]:# type: ignore# Make sure that value is a UUIDv4# If not, raise an error# This is to prevent UUIDv1 or any other version from being stored in the databaseifself._binary_compression:ifnotisinstance(value,UUID):raiseValueError("UUIDField only accepts UUID values")returnvalue.bytesreturnvalueandstr(value)