Skip to content

Add endpoint-specific join_config support to crud_router #283

@igorbenav

Description

@igorbenav

Discussed in #247

Originally posted by seklmco September 3, 2025
Hello. To start off, I have to admit this library is an amazing time saver so thank you for creating it.

I want to learn about how to define a custom response model at the router level (crud_router). I see that there is the select_schema parameter. However, when I define it to be something like LibraryRead, it is trying to read a relationship field from the database, which does not exists. Take a look at these models as an example (I'm using sqlmodel):

class LibraryBase(SQLModel):
    name: str = Field()

class Library(LibraryBase, table=True):
    id: str = Field(primary_key=True)
    books: list[Book] = Relationship(back_populates='libraries')

class LibraryRead(LibraryBase):
    id: str
    books: list[Book]

I would then want LibraryRead to be the response model so FastAPI can automatically respond with the appropriate data. With FastAPI, I would define the endpoint like this and the books list would be included in the response:

@router.get("", response_model=LibraryRead
def get_libraries(...):
    libraries = get_libraries(...) # Returns a list of Library objects
    return libraries # FastAPI automatically returns list of LibraryRead objects because of the response_model

However, when I set the select_schema to LibraryRead, it looks like it is attempting to read books from the database. Which I suppose makes sense but not the functionality I am looking for.

library_router_ = crud_router(
    session=get_async_session,
    model=Library,
    crud=FastCRUD(Library),
    path="/libraries",
    tags=["Library"],
    select_schema=LibraryRead
)

If I don't set select_schema then the response, as expected, only includes id and name fields.

Let me know what I am missing. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions