Working with Multi-Select Properties in Notion API
I’m building a Discord bot that creates database entries in Notion. I’m using the notion-py package to handle the API calls.
Here’s my current setup for adding new records:
def create_database_entry():
view = client.get_collection_view(database_urls[current_type])
new_entry = view.collection.add_row()
new_entry.website_url = stored_url
new_entry.entry_title = stored_title
if current_type == 'articles':
new_entry.category = stored_category
labels = stored_labels.split(',')
for label in labels:
setup_multiselect_option("Labels", label)
else:
new_entry.section = stored_category
The issue I’m facing is with populating the Labels column which has a multi-select property type. I found a community solution and modified it:
label_options = ['technology', 'programming']
def setup_multiselect_option(column_name, option_value, color=None):
if color is None:
color = choice(label_options)
schema = view.collection.get(["schema"])
column_config = next(
(config for key, config in schema.items() if config["name"] == column_name), None
)
if not column_config:
raise ValueError(f'Column "{column_name}" not found!')
if column_config["type"] != "multi_select":
raise ValueError(f'Column "{column_name}" is not multi-select type!')
existing = next(
(item for item in column_config["options"] if item["value"] == option_value), None
)
if existing:
raise ValueError(f'Option "{option_value}" already exists!')
column_config["options"].append({
"id": str(uuid1()),
"value": option_value,
"color": color
})
collection.set("schema", schema)
However, this throws an error: HTTPError: Unsaved transactions: Not allowed to edit column: schema
How can I properly assign values to a multi-select column in notion-py? What’s the correct approach for this?