-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
72 lines (63 loc) · 1.71 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import uuid
from typing import Optional
# from fastapi.encoders import jsonable_encoder # -- maybe useful, instead of selfmade ?field? "json_encoders"
from pydantic import BaseModel, Field
"""
class ObjId(ObjectId):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v: str):
try:
return cls(v)
except:
raise ValueError("Not a valid ObjectId")
"""
class Person(BaseModel):
id: str = Field(default_factory=uuid.uuid4, alias="_id")
name: str = Field(...)
age: str = Field(...)
gender: str = Field(...)
class Config:
allow_population_by_field_name = True
schema_extra = {
"example": {
"_id": "066de609-b04a-4b30-b46c-32537c7f1f6e",
"name": "Don",
"age": "15",
"gender": "M"
}
}
"""
class Person(BaseModel):
id: ObjId = Field(default_factory=uuid.uuid4, alias="_id")
name: str = None
age: int = None
gender: str = None
class Config:
allow_population_by_field_name = True
schema_extra = {
"example": {
"_id": "ObjectId('6411d24e8902d0564fca82a0')",
"name": "Don",
"age": "15",
"gender": "M"
}
}
json_encoders = {
ObjId: lambda v: f"ObjectId('{str(v)}')",
}
"""
class PersonUpdate(BaseModel):
name: Optional[str]
age: Optional[int]
gender: Optional[str]
class Config:
schema_extra = {
"example": {
"Name": "Don",
"age": "16",
"gender": "M"
}
}