'FastAPI pagination query response excludes item id and date

I'm trying to implement pagination in my FastAPI-application. I want to display all todo's that a user has added. The problem is that the response I'm getting back is omitting some of the fields that I have defined in my db-model (i.e. fields: created_at, updated_at, owner_id and id). This is what I want the response to look like:

{
  "items": [
      {
          "id": 2,
          "category": "House",
          "created_at": "2022-04-22T08:23:20",
          "owner_id": 1,
          "priority": 1,
          "todo": "clean",
          "updated_at": null
      }
  ],
  "total": 1,
  "page": 1,
  "size": 5
}

But this is what I currently receive when calling the endpoint:

{
    "items": [
        {
            "category": "House",
            "priority": 1,
            "todo": "clean"
        }
    ],
    "total": 1,
    "page": 1,
    "size": 5
}

Below you can see my code:

class Todos(Base):
    __tablename__ = "todos"

    id = Column(Integer, primary_key=True, index=True)
    todo = Column(String)
    category = Column(String)
    priority = Column(Integer)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    owner_id = Column(Integer, ForeignKey("users.id"))
    owner = relationship("Users", back_populates="todos")
    

class Todo(BaseModel):
    todo: str
    category: str
    priority: int



@router.get(
    "/",
    status_code=status.HTTP_200_OK,
    response_model=Page[Todo],
)
async def paginated_todos(
    params: Params = Depends(),
    user: dict = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    if user is None:
        raise get_user_exception()
    todo_model = (
        db.query(models.Todos)
        .filter(models.Todos.owner_id == user.get("id")).all()
    )

    params.size = int(os.environ["PAGE_SIZE"])
    if todo_model is not None:
        return module_paginate(jsonable_encoder(todo_model), params)

    raise http_exception()

When I do a breakpoint() in my code and print module_paginate(jsonable_encoder(todo_model), params) I am able to access all the fields, so there is something going on when the data is returned to whatever client I’m using to call the endpoint (have tried with Postman and built-in swagger/docs). Does anyone have any ideas why this is occurring?



Solution 1:[1]

You've defined the response_model as Page[Todo] - your definition of Todo includes todo, category and priority (I'm guessing Page is your pagination wrapper):

class Todo(BaseModel):
    todo: str
    category: str
    priority: int

These fields define what gets returned for each Todo in your response:

"category": "House",
"priority": 1,
"todo": "clean"

If you want to include other fields extend Todo with the fields you want added to your response:

class Todo(BaseModel):
    todo: str
    category: str
    priority: int

    created_at: datetime.datetime
    updated_at: Optional[datetime.datetime]

.. etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MatsLindh