I would like to create a custom UUID field in peewee (via MySQL).
In python, I use UUID as a hexadecimal string, for example:
uuid = '110e8400-e29b-11d4-a716-446655440000'
But I want to save it in the database in the type column in BINARY(16)order to save space.
MySQL has a built-in methods HEX()and UNHEX()for converting between string and binary code.
So my question is, how can I say that peewee generates SQL that uses an inline function? Here is the idea for the code I want to work:
class UUIDField(Field):
db_field='binary(16)'
def db_value(self, value):
if value is not None:
uuid = value.translate(None, '-')
def python_value(self, value):
if value is not None:
, , peewee SQL. , , , python, SQL.
EDIT: , , python. , , !
import binascii
from peewee import *
db = MySQLDatabase(
'database',
fields={'binary(16)': 'BINARY(16)'}
)
class UUIDField(Field):
db_field='binary(16)'
def db_value(self, value):
if value is None: return None
value = value.translate(None, '-')
value = binascii.unhexlify(value)
return value
def python_value(self, value):
if value is None: return None
value = '{}-{}-{}-{}-{}'.format(
binascii.hexlify(value[0:4]),
binascii.hexlify(value[4:6]),
binascii.hexlify(value[6:8]),
binascii.hexlify(value[8:10]),
binascii.hexlify(value[10:16])
)
return value