[英]Django Ninja API framework Pydantic schema for User model ommits fields
Project running Django with Ninja API framework.使用 Ninja API 框架运行 Django 的项目。 To serialize native Django's User model I use following Pydantic schema:要序列化本机 Django 的用户 model,我使用以下 Pydantic 架构:
class UserBase(Schema):
"""Base user schema for GET method."""
id: int
username = str
first_name = str
last_name = str
email = str
But, this approach gives me response:但是,这种方法给了我回应:
{
"id": 1
}
Where are the rest of fields?字段rest在哪里?
Thought this approach gives me a full data response:以为这种方法给了我一个完整的数据响应:
class UserModel(ModelSchema):
class Config:
model = User
model_fields = ["id", "username", "first_name", "last_name", "email"]
Response from ModelSchema:来自 ModelSchema 的响应:
{
"id": 1,
"username": "aaaa",
"first_name": "first",
"last_name": "last",
"email": "a@aa.aa"
}
Looks like the problem is that you didn't specify type for other fields.看起来问题是您没有为其他字段指定类型。 Just replace =
with :
in your schema for all fields:只需在所有字段的模式中将=
替换为:
:
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.