Hi!
I would like to handle PostgreSQL BYTEA data. For that I need to be able to read hexadecimal encoded data such as: '\x0b10100991..'
How would I extend the BaseModel to support this type correctly? Something like this is not working:
from pydantic import BaseModel,
class bytea(BaseModel):
whatever: str
class ExampleBase(BaseModel):
atdb: bytea = None
class Config:
orm_mode = True
json_encoders = {
bytea: lambda v: bytes.fromhex(v).decode('utf-8')
}
class ExampleCreate(ExampleBase):
pass
Implement a Custom data type or use validators.
As future reference:
class ByteA:
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not isinstance(v, bytes):
raise ValueError(f'`bytes` expected not {type(v)}')
return binascii.b2a_hex(v)
Looks good, except:
bytea should definitely not inherit from BaseModelRight, I have updated it accordingly. Thank you Samuel!