简体   繁体   中英

Django Ninja API framework Pydantic schema for User model ommits fields

Project running Django with Ninja API framework. To serialize native Django's User model I use following Pydantic schema:

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?

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:

{
  "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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