提问人:Nilesh 提问时间:9/30/2023 最后编辑:Brian Tompsett - 汤莱恩Nilesh 更新时间:10/2/2023 访问量:95
返回空数据的 FastAPI 端点
FastAPI Endpoint Returning Empty Data
问:
我正在开发一个 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
答: 暂无答案
评论