小编Taz*_*bar的帖子

如何从 pydantic 模型中的数据库中仅返回一列作为列表?

我有一个简单的数据库关系,用于存储哪个用户喜欢哪个帖子。但在 like 属性中,它返回一个字典列表,其中包含post_id但我想返回app_ids.

这是我的pydantic模型:

class PostLiked(BaseModel):
    post_id: str

    class Config:
        orm_mode = True

class ShowUser(BaseModel):
    public_id: str
    name: str
    username: str
    email: str
    number_of_apps: int
    is_developer_account: bool
    profile_image_url: Any or str
    created_account_on: Any or str
    liked: List[AppLiked]

    class Config:
        orm_mode = True```

Run Code Online (Sandbox Code Playgroud)

这是它返回的内容:

{
  "public_id": "0bdf790b",
  "name": "Tazim Rahbar",
  "username": "tazim404",
  "email": "rahbartazim@gmail.com",
  "number_of_post": 1,
  "is_developer_account": true,
  "profile_image_url": "https://picnicss.com/img/basket.png",
  "created_account_on": "2021-08-15T18:49:35.367674",
  "liked": [
    {
      "post_id": "70a31f76"
    },
    {
      "post_id": "2674a446"
    },
    {
      "post_id": "dd977cc8" …
Run Code Online (Sandbox Code Playgroud)

python pydantic fastapi

7
推荐指数
1
解决办法
694
查看次数

标签 统计

fastapi ×1

pydantic ×1

python ×1