如何按显示顺序获取数据库的行?

fei*_*ong 15 python notion-api

我有一些 Python 代码,可以使用notion-client 库检索 Notion 数据库中的行。该代码确实设法检索所有行,但顺序错误。我查看了API 参考中的Sort 对象,但无法弄清楚如何使用它以行在 notion.so 上显示的确切顺序返回行。这是有问题的片段:

from notion_client import Client

notion = Client(auth=NOTION_API_TOKEN)
result = notion.databases.query(database_id='...')
for row in result['results']:
  title = row['properties']['NAME_OF_PROPERTY']['title']
  if len(title) == 0:
    print('')
  else:
    print(title[0]['plain_text'])
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

Bar*_*mar -1

使用order参数notion.databases.query(). 该参数是排序规范的列表,即字典。

result = notion.databases.query(
    database_id = 'df4dfb3f-f36f-462d-ad6e-1ef29f1867eb',
    sort = [{"property": "NAME_OF_PROPERTY", "direction": "ascending"}]
)
Run Code Online (Sandbox Code Playgroud)

您可以在列表中放置多个排序规范,后面的排序规范将用于前面属性相同的行。

  • @Barmar 一般来说,对于关系数据库来说,这是事实,但在概念视图中,(如果没有设置排序)您可以手动拖动项目以进行自定义排序。我猜想它存储在视图内部的某个地方,并且根据“adlopez15”的答案查询视图——由于 API 限制,目前不可能。 (5认同)
  • 是的,我尝试过这个,但是我该为“属性”输入什么?我不想按任何特定属性(列)排序,我只想按它们在表中出现的顺序排序。就我而言,向表中添加与显示顺序对应的另一列对我来说是不切实际的。 (2认同)