Fastapi pydantic 相关架构响应按列过滤
我正在使用 fastapi pydantic 模式获取发票请求 api 及其相应项目。 API 工作正常,但考虑到带有发票 ID 的所有相关项目行。但我希望根据某些列过滤外键相关数据。与 is_active == True
类似,仅考虑那些发票项目行。
{
'id': 1
'invoice_number': 1
'item_name': 'this is invoice one'
'invoice_items': [
{'invoice_id': 1, 'item_name': item one, deleted_at: None},
{'invoice_id': 1, 'item_name': item two, deleted_at: None},
{'invoice_id': 1, 'item_name': item three, deleted_at: None}
]
}
I am using fastapi pydantic schema for get request apis to invoice and its corresponding items. The api is working fine but considering all the related rows of items with invoice_id. But I want the foreign key related data to be filtered based on some column. Like where is_active == True
, consider only those invoice item rows.
{
'id': 1
'invoice_number': 1
'item_name': 'this is invoice one'
'invoice_items': [
{'invoice_id': 1, 'item_name': item one, deleted_at: None},
{'invoice_id': 1, 'item_name': item two, deleted_at: None},
{'invoice_id': 1, 'item_name': item three, deleted_at: None}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
I just stumbled upon your question.
您不应该在 pydantic 模式中进行过滤。
您可以在将数据提供给响应之前过滤数据。
从数据库中获取信息后,您可以执行以下操作:
然后您的发票将如下所示,您可以将该
发票
作为回复:我希望此建议对您有所帮助。
祝你好运!
I just stumbled upon your question.
You shouldn't do that filtering in pydantic schema.
You can filter data before you give that data to response.
You can do something like this after you got information from your database:
then your invoice will look like this and you can give that
invoice
as response:I hope this advice will be helpful for you.
Wish you good luck!