Django Ninja API framework Pydantic schema for User model ommits fields

Question:

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]"
}
Asked By: Vitalii Mytenko

||

Answers:

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
Answered By: neverwalkaloner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.