23 lines
452 B
Python
23 lines
452 B
Python
from typing import Optional, Union
|
|
|
|
from bson import ObjectId
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
# mongodb _id 类型
|
|
class OId(ObjectId):
|
|
@classmethod
|
|
def __get_validators__(cls):
|
|
yield cls.validate
|
|
|
|
@classmethod
|
|
def validate(cls, v):
|
|
try:
|
|
return ObjectId(v)
|
|
except:
|
|
raise ValueError('无效的格式')
|
|
|
|
|
|
class DBBase(BaseModel):
|
|
id: Union[OId, str] = Field(None, alias='_id')
|