S
S
sir-coffee2021-08-28 21:35:50
Python
sir-coffee, 2021-08-28 21:35:50

How to populate a column with multi-select type in notion-py(notion-api)?

I am creating a telegram bot that saves notes to a notion table, for this I use these libraries:

  • notion-py
  • pyTelegramBotAPI

At the beginning, I connect to notion using token_v2, then I write the bot itself, which accepts all note data. After that, I save a note on notion like so:
def make_notion_row():
    collection_view = client.get_collection_view(list_url[temporary_category]) #take collection
    print(temporary_category)
    print(temporary_name)
    print(temporary_link)
    print(temporary_subcategory)
    print(temporary_tag)
    row = collection_view.collection.add_row() #make row
    row.ssylka = temporary_link #this is link
    row.nazvanie_zametki = temporary_name #this is name
    if temporary_category == 0: #this is category, where do I want to save the note
        row.stil = temporary_subcategory #this is subcategory
        tags = temporary_tag.split(',') #temporary_tags is text that has many tags separated by commas. I want to get these tags as an array
        for tag_one in tags:
            add_new_multi_select_value("Теги", tag_one) 
#"Теги" is "Tag column" in russian. in this situation, 
# tag_one takes on the following values: ['my_hero_academia','midoria']
    else:
        row.kategoria = temporary_subcategory

There are no problems with the creation of a row on a notion using this function:

612a813956924163881297.png

The problem lies in filling the 'Tags' column, which is of the 'multi-select' type.

Trying to solve this problem, I came across a bkiac user script that adds the ability to populate a 'multi-select' type column, but even his code was not perfect (apparently the notion-py author updated his library), I modified it a bit.
By the way, in the official notion-py documentation didn't have anything about 'multi-select'

Here is the same function:
art_tags = ['ryuko_matoi', 'kill_la_kill']
def add_new_multi_select_value(prop, value, style=None):global temporary_prop_schema
   ​if style is None:
       ​style = choice(art_tags)

   ​collection_schema = collection_view.collection.get(["schema"])
   ​prop_schema = next(
       ​(v for k, v in collection_schema.items() if v["name"] == prop), None
   ​)
   ​if not prop_schema:
       ​raise ValueError(
           ​f'"{prop}" property does not exist on the collection!'
       ​)
   ​if prop_schema["type"] != "multi_select":
       ​raise ValueError(f'"{prop}" is not a multi select property!')

   ​dupe = next(
       ​(o for o in prop_schema["options"] if o["value"] == value), None
   ​)
   ​if dupe:
       ​raise ValueError(f'"{value}" already exists in the schema!')
   ​temporary_prop_schema = prop_schema
   ​prop_schema["options"].append(
       ​{"id": str(uuid1()), "value": value, "style": style}
   ​)
   ​collection.set("schema", collection_schema)


But when run, the code throws an error because of this very function (add_new_multi_select_value).
The code swears at the column: Here is the error itself:
collection.set("schema", collection_schema)
add_new_multi_select_value("Теги","my_hero_academia)
Traceback (most recent call last):
 ​File "<pyshell#4>", line 1, in <module>
   ​add_new_multi_select_value("Теги","my_hero_academia)
 ​File "C:\Users\sir-coffee\OneDrive\Documents\Programming\Other\notion-bot\program\notionbot\test.py", line 53, in add_new_multi_select_value
   ​collection.set("schema", collection_schema)
 ​File "C:\Users\sir-coffee\AppData\Local\Programs\Python\Python39-32\lib\site-packages\notion\records.py", line 115, in set
   ​self._client.submit_transaction(
 ​File "C:\Users\sir-coffee\AppData\Local\Programs\Python\Python39-32\lib\site-packages\notion\client.py", line 290, in submit_transaction
   ​self.post("submitTransaction", data)
 ​File "C:\Users\sir-coffee\AppData\Local\Programs\Python\Python39-32\lib\site-packages\notion\client.py", line 260, in post
   ​raise HTTPError(
requests.exceptions.HTTPError: Unsaved transactions: Not allowed to edit column: schema

Unsaved transactions: Not allowed to edit column
: schema

Honestly, at this stage of solving the problem, I don’t know what to do, I reviewed all the forums about notion-py, and did not find something similar.

In the end, two polar questions remained: How to populate a column with a multi-select type in notion-py? or How to get access to edit column - schema?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sir-coffee, 2021-09-05
@sir-coffee

I solved this problem with this wonderful code:

row.set_property("Категория", temporary_subcategory)

take fun!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question