【问题标题】:Updating multiple entries in a notion database with API使用 API 更新概念数据库中的多个条目
【发布时间】:2022-10-20 23:32:27
【问题描述】:

我有一个概念数据库,我用 API 调用它,如下所示:

url = f"https://api.notion.com/v1/databases/{NotionDB}/query"
payload = {"page_size": 200}
headers = { "Authorization": NotionKEY,
            "accept": "application/json",
            "Notion-Version": "2022-06-28",
            "content-type": "application/json"
        }
response = requests.post(url, json=payload, headers=headers)
data = json.loads(response.text)

这将返回具有以下结构的 data['results']

[{'object': 'page',
  'id': 'ABC'
  ....
  'parent': {'type': 'database_id',
   'database_id': 'XXXXX'},
  'archived': False,
  'properties': {'Prop 1': {'id': '%40QKL',
   ....}},
  'url': 'https://www.notion.so/XYZ'},
 {'object': 'page',
  'id': 'CDE',
  'parent': {'type': 'database_id',
   'database_id': 'XXXXX'},
  'archived': False,
  'properties': {'Prop 1': {'id': '%40QKL',
    ..... }},
  'url': 'https://www.notion.so/ABCD'},
{'object': etc}, '{'object': etc}]

所以,一个对象列表,每个对象似乎都是一个页面。我的目标是对这些对象的属性进行一些更改,然后更新数据库。

我可以循环遍历每个单独的页面。编辑 jsons 列表后,我这样做:

url = "https://api.notion.com/v1/pages/ID-OF-i_th-PAGE"
payload = {"properties": data['results'][i]['properties']}
headers = {
     "Authorization": NotionKEY,
    "accept": "application/json",
    "Notion-Version": "2022-06-28",
    "content-type": "application/json"
}
response = requests.patch(url, json=payload, headers=headers)

哪个有效。但是,如果我的列表有 N 元素,这意味着执行 N API 调用。有没有办法在一次调用中直接更新数据库元素的属性?

【问题讨论】:

  • 您想修改字典的键或值吗?
  • 字典中的值。钥匙应该留下来。
  • 您想修改键 proprieties 的嵌套字典吗? 'properties': {'Prop 1': {'id': '%40QKL', ....}?
  • 我想修改属性的值。访问此类值的示例可能是 data['results'][0]['properties']['Prop 1']['rich_text'][0]['text']['content']

标签: python json notion-api notion


【解决方案1】:

根据 Notion 支持,这是不可能的。已请求该功能,但在2022 年 10 月 20 日.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    相关资源
    最近更新 更多