Reference API URL : Update database
Update database를 해보겠습니다. database
의 title
이나 properties
속성을 변경할 수 있습니다.
database_id
는 search
를 통해서 조회해 오시면 됩니다.databases = notion.search(filter={"property": "object", "value": "database"})
pprint(databases)
필요한 값은 database_id
입니다.
'results': [{'archived': False,
...
'id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
...
'properties': {'메모': {'id': '%5C%3D%7B%5E',
'name': '메모',
'rich_text': {},
'type': 'rich_text'},
'상태': {'id': '%3EQQn',
'name': '상태',
'select': {'options': [
{'color': 'green',
'id': '8e6092bc-4dcf-42fc-ae54-5e6bf1586c48',
'name': '★★★'},
{'color': 'purple',
'id': 'ed26ef07-df97-4ced-9275-9aa0c71d5bb1',
'name': '★★'},
{'color': 'gray',
'id': '632758dd-d74c-455c-8a3b-0d063282a3a7',
'name': '★'}]},
'type': 'select'},
'이름': {'id': 'title',
'name': '이름',
'title': {},
'type': 'title'}},
'title': [{'annotations': {'bold': False,
'code': False,
'color': 'default',
'italic': False,
'strikethrough': False,
'underline': False},
'href': None,
'plain_text': '피자 맛집 리스트',
'text': {'content': '피자 맛집 리스트', 'link': None},
'type': 'text'}]
이제 database_id
를 가져오겠습니다. 첫번째([0]
)의 database
의 id
를 가져옵니다.
database_id = databases['results'][0]['id']
title
은 array 즉 list
형태여야 합니다.title_value = [{'type': 'text', 'text': {'content': '동네맛집 리스트', 'link': None}}]
properties
에는 수정할 값을 넣어주면 됩니다.
예를들면 나는 별 4개짜리 옵션을 더 추가해주고 싶다라고 하면 위 상태의 옵션 값에서 4개짜리 옵션을 추가하여 값을 입력해줍니다.
select_property = {'평점': {'name': '평점',
'select': {'options': [{'color': 'red', 'name': '★★★★'}]},
'type': 'select'}
}
기존 '평점'
property
의 option
값도 가져오겠습니다.
properties = databases['results'][0]['properties']
property_score = properties['평점']
pprint(property_score)
property_score
를 찍어보니 이렇게 값이 나왔네요.
{'id': '%3EQQn',
'name': '평점',
'select': {'options': [{'color': 'green',
'id': '8e6092bc-4dcf-42fc-ae54-5e6bf1586c48',
'name': '★★★'},
{'color': 'purple',
'id': 'ed26ef07-df97-4ced-9275-9aa0c71d5bb1',
'name': '★★'},
{'color': 'gray',
'id': '632758dd-d74c-455c-8a3b-0d063282a3a7',
'name': '★'}]},
'type': 'select'}
여기서 필요한 값은 options
값입니다. 'options'
값만 변수에 넣도록 하겠습니다. 그런뒤 list
형태의 select_property
변수와 합치도록 하겠습니다.
option_value = property_score['select']['options']
select_property['평점']['select']['options'].extend(option_value)
이제 update
를 호출하시면 입력한 값 대로 업데이트가 진행됩니다.
id 값은 기존 값과 동일할것 이기 때문에 별도로 del
로 'option'
키의 'id'
값을 지워주실 필요는 없습니다.
notion.databases.update(database_id=database_id, title=title_value, properties=select_property)
기존에 있는 값에 'color'
가 다를 경우 update
로 변경이 불가합니다.
notion_client.errors.APIResponseError: Cannot update color of select with name: ★★★
'★★★'
는 'green'
으로 지정되어 있으므로, 반드시 'green'
으로 update
를 해야됩니다.