返回空数据的 FastAPI 端点

FastAPI Endpoint Returning Empty Data

提问人:Nilesh 提问时间:9/30/2023 最后编辑:Brian Tompsett - 汤莱恩Nilesh 更新时间:10/2/2023 访问量:95

问:

我正在开发一个 FastAPI 端点 (/all),它应该从数据库中获取数据并返回它作为响应。但是,我遇到了一个问题,即与数据库中存储的数量相比,响应数据非常少(数据库区域中的潜在客户约为 59799,但 Swagger UI 仅提供 1209 个潜在客户的输出),我不知道为什么。 PS:我试图解决问题,因此评论了一堆代码。 此块可能会解决该问题,但分页正在被禁用。

# query = text(query)
# x = print("SQL Query:", query)
# result = db.get_db().execute(query)
# columns = result.keys()  # Get the column names
# data = [dict(zip(columns, row)) for row in result.fetchall()]
@router.post("/all", operation_id="get_all_leads")
def get_all_leads(
    paging: paging = None,
    assigned: int | None = None,
    source: int | None = None,
    status: str | None = None,
    additional_filters: int | None = None,
    username: User = Depends(get_current_account),
    customer_id: Annotated[str | None, Header()] = None
):
    '''
    This api gets all the leads 
    
    parameters
    -------
    
    - filter text -> the text you want to search for 
    - limit_per_page -> the amount of data that should be fetched in a single page 
    - order by -> order the data by the column name  
    - skip -> skips the data (eg : 0,10,20,30)
    - sort -> sort the column (1 : asc , -1 : desc)  
    
    filters 
    -------
    
    - assigned : id of assigned staff
    - source  : source id 
    - status : csv of status id (eg : 1,7,4,3)
    - additional_filters : addition filter for the DataTable
    '''
    try:
        if paging is None:
            paging = paging()
        
        db = LoadDatabase(customer_id)

        filters = {'assigned': assigned,
                'source': source,
                "status": status,
                "additional_filters": additional_filters
                }

        query = fetch_leads_all(db.get_db(), single_lead=None, filters=filters)
        # query = text(query)
        # x = print("SQL Query:", query)
        # result = db.get_db().execute(query)
        # columns = result.keys()  # Get the column names
        # data = [dict(zip(columns, row)) for row in result.fetchall()]  # Convert rows to dictionaries
        paging_params = df_datatables(dataframe=None,
                                    sql=query,
                                    db_connection=db.get_engine(),
                                    params={
                                        'paging':paging,
                                        'filter': paging.filter_text,
                                        'order_by': paging.order_by,
                                        'sort_asc': assign_order(paging.sort),
                                        'skip': paging.skip,
                                        'limit_per_page': paging.limit_per_page,
                                        "exclude_column_search": []
                                    }
                                    )

        data = []  # Initialize the data variable as an empty list
        #output = paging_params.get_data()
        data = output["dataframe"].to_dict(orient="records")

        return {
            "status": "success",
            "total_record": output["total_record"],
            "data": data
        }

    except HTTPException as e:
        raise e
python-3.x 函数 fastapi web-development-server

评论

2赞 M.O. 9/30/2023
您是否使用 cURL 或类似工具进行过测试?Swagger UI 在处理大量数据方面并不出色。
0赞 MatsLindh 10/1/2023
此外,首先查看 API 在调试器中实际返回的数据 - 在开始时设置断点,并确认您实际获取和返回的行数与您认为的行数一样多 - 缩小引入错误的可能位置
0赞 Nilesh 10/4/2023
我也尝试了cURL,但没有用,得到了相同的结果。回复 M.O.
0赞 Nilesh 10/4/2023
我不明白该怎么做@MatsLindh

答: 暂无答案