Reference API URL : Update database
Let's update
the database
. We can change the 'title'
of 'database'
or 'properties'
value with this method.
database_id
through the search
method.databases = notion.search(filter={"property": "object", "value": "database"})
pprint(databases)
We need the database_id
value.
'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'}]
Now try to bring database_id
. Get the id
of the first([0]
th) database.
database_id = databases['results'][0]['id']
title
must be an array which means to be list
form.title_value = [{'type': 'text', 'text': {'content': '동네맛집 리스트', 'link': None}}]
Put the value we want to modify into properties
.
For example, I want to add the "4 stars" option and add more an option value from the above status.
select_property = {'평점': {'name': '평점', # 평점 means score
'select': {'options': [{'color': 'red', 'name': '★★★★'}]},
'type': 'select'}
}
Try to get the option
of properties
that is pre-existing value '평점'
(score).
properties = databases['results'][0]['properties']
property_score = properties['평점']
pprint(property_score)
Print property_score
then value shows like this.
{'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'}
Here, the needed value is the options
value. Put the 'options'
value into a variable. And then combine with select_property
, which is a list
form.
option_value = property_score['select']['options']
select_property['평점']['select']['options'].extend(option_value)
Now call to update
, then API will update as following from the inputted values. Id value will be identical to pre-existing value, so you don't need to do the del
to 'id'
value of the 'option'
key.
notion.databases.update(database_id=database_id, title=title_value, properties=select_property)
If 'color'
differs from pre-existing values, update
is impossible.
notion_client.errors.APIResponseError: Cannot update color of select with name: ★★★
'★★★'
is set as the 'green'
color, so you must update the 'green'
color when the value is '★★★'
.