-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
38 lines (29 loc) · 832 Bytes
/
models.py
File metadata and controls
38 lines (29 loc) · 832 Bytes
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
from pydantic import BaseModel, Field
from pymongo import MongoClient
from bson import ObjectId
from typing import Optional
myclient = MongoClient('mongodb://localhost:27017/')
db = myclient["NFTGO"]
class PyObjectId(ObjectId):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not ObjectId.is_valid(v):
raise ValueError('Invalid objectid')
return ObjectId(v)
@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(type='string')
class User(BaseModel):
id: Optional[PyObjectId] = Field(alias='_id')
u_id: str
name: str
username: str
password: str
class Config:
arbitrary_types_allowed = True
json_encoders = {
ObjectId: str
}