1

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"
}
metersk
  • 11,803
  • 21
  • 63
  • 100
Vitalii Mytenko
  • 544
  • 5
  • 20

1 Answers1

1

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
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100