fastcrud icon indicating copy to clipboard operation
fastcrud copied to clipboard

Rename "data" to "items" in multi response

Open feluelle opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

The multi-response field data is generic and doesn't describe the returned data very well.

Describe the solution you'd like

I would like to use a more descriptive term e.g. items that indicate that a list is being returned.

feluelle avatar Sep 03 '24 08:09 feluelle

Another option might be to make the key configurable on the FastCRUD instance.

ianbuss avatar Sep 03 '24 08:09 ianbuss

here is my current solution

SchemaType = TypeVar("SchemaType", bound=BaseModel)
class ListResponse(BaseModel, Generic[SchemaType]):
    items: list[SchemaType]


class PaginatedListResponses(ListResponse[SchemaType]):
    total_count: int
    has_more: bool
    page: Optional[int] = None
    items_per_page: Optional[int] = None

@router.get("/foods", response_model=PaginatedListResponses[FoodRead])
async def get_foods(
    db: Annotated[AsyncSession, Depends(async_get_db)],
    page: int = 1,
    items_per_page: int = 10,
):
    food_lists = await crud_foods.get_multi(
        db=db,
        offset=compute_offset(page, items_per_page),
        limit=items_per_page,
    )
    response: dict[str, Any] = paginated_response(
        crud_data=food_lists, page=page, items_per_page=items_per_page
    )

    if 'data' in response:
        response['items'] = response.pop('data')   
    return response

sombathoudom avatar Oct 28 '24 09:10 sombathoudom

I've created a possible solution in pull request. https://github.com/igorbenav/fastcrud/pull/203

I'd like you to check it and if you know how to improve it - share a comment.

doubledare704 avatar Feb 24 '25 11:02 doubledare704

Closed by #203

igorbenav avatar Jun 09 '25 01:06 igorbenav