Home > Net >  Django Ninja API framework Pydantic schema for User model ommits fields
Django Ninja API framework Pydantic schema for User model ommits fields

Time:11-23

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": "[email protected]"
}

CodePudding user response:

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
  • Related