繁体   English   中英

Django Ninja API 用户框架 Pydantic 模式 model 省略字段

[英]Django Ninja API framework Pydantic schema for User model ommits fields

使用 Ninja API 框架运行 Django 的项目。 要序列化本机 Django 的用户 model,我使用以下 Pydantic 架构:

class UserBase(Schema):
    """Base user schema for GET method."""

    id: int
    username = str
    first_name = str
    last_name = str
    email = str

但是,这种方法给了我回应:

{
  "id": 1
}

字段rest在哪里?

以为这种方法给了我一个完整的数据响应:

class UserModel(ModelSchema):
    class Config:
        model = User
        model_fields = ["id", "username", "first_name", "last_name", "email"]

来自 ModelSchema 的响应:

{
  "id": 1,
  "username": "aaaa",
  "first_name": "first",
  "last_name": "last",
  "email": "a@aa.aa"
}

看起来问题是您没有为其他字段指定类型。 只需在所有字段的模式中将=替换为:

class UserBase(Schema):
    """Base user schema for GET method."""

    id: int
    username: str # not =
    first_name: str
    last_name: str
    email: str

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM