使用PonyORM和FastAPI列出

2024-04-25 14:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在PonyORM管理的db中保存一个具有列表作为属性的类,但显然PonyORM不支持列表,所以我尝试使用StrArray来保存,但它不能。有没有人有更好的办法?其思想是,Game类有一个chat属性,用于用字符串列表对其进行建模

ENDPOINT:

@app.post("/game/", response_model=Game, status_code=201)
def create_game(game: Game):
    with db_session:
        if db.Game.exists(gameName=game.gameName):
            raise HTTPException(status_code=400, detail="Duplicate")
        return db.Game(**game.dict()).to_dict()

MODEL:

    class Game(BaseModel):
        gameName: str
        password: str
        numPlayers: int
        players: Set[Player]
        gameStatus: GameStatus
        chat: List[str]

ENTITY:

class Game(db.Entity):
    id = PrimaryKey(int, auto=True)
    gameName = Required(str, unique=True)
    password = Required(str)
    numPlayers = Required(int)
    players = Set(Player)
    gameStatus = Required(GameStatus)
    chat = Required(StrArray)

ERROR:
  File "/home/nibblex/Descargas/SV/venv/lib/python3.7/site-packages/pydantic/schema.py", line 646, in add_field_type_to_schema
    if issubclass(field_type, type_):
TypeError: issubclass() arg 1 must be a class

Tags: game列表db属性typestatuschatrequired