Notion API master series 20 - Analysis - Create a database

소찬 (Chan)·2022년 11월 29일
0
post-thumbnail

Reference API URL : Create a database

First parameter value is 'parent'.

  • parent (json) required
    A page parent

'parent' value is from 'parent' of 'page'. In other words, creating the 'parent' page is available.
It is unavailable 'parent' database to create a database.
If you want to get page_id, use the search method.
Set as filter={"property": "object", "value": "page"} value, call search, then it shows page list which API Bot has right.

pages = notion.search(filter={"property": "object", "value": "page"}

We will bring page_id for adding value from here.

 'results': [{'archived': False,
              ... ,
              'icon': {'emoji': '🍕', 'type': 'emoji'},
              'id': '1f217cc1-225f-4580-8e22-cd7eb8db3c5e',
              ... ,
              'object': 'page',
              'parent': {'database_id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
                         'type': 'database_id'},

See 'database_id' of 'parent', we need value which is 'id' value of 'page'. Follow from above list, bring ['id'] value from [0]th.

page_id = pages['results'][0]['id']
parent_id = {'type': 'page_id', 'page_id': page_id}
  • title (array)
    Title of database as it appears in Notion. An array of rich text objects.
    Set 'title' value. It is an array, so put the data in list form.
title_value = {'type': 'text', 'text': {'content': '피자 토핑 목록', 'link': None}}

The value is a pizza topping, so set the icon value. It is optional, so unnecessary to set.

icon_value = {'emoji': '🍕', 'type': 'emoji'}
  • properties (json) required
    Property schema of database. The keys are the names of properties as they appear in Notion and the values are property schema objects.
    The properties are also necessary to value.

Refer to Property Schema Object document. It explains how to write the form.
It's good to fill the code by referring to this document. The best way is to refer to the 'properties' of 'database'. Then let's check pre-existing properties.

{'archived': False,
 ... ,
 'icon': {'emoji': '🍕', 'type': 'emoji'},
 'id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
 ... ,
 'properties': {'상태': {'id': '%3EQQn',
                        'name': '상태',
                        'select': {'options': [{'color': 'green',
                                                'id': '6da478a3-5307-4d77-a47a-934a86ddda48',
                                                'name': '★★★★★'},
                                               {'color': 'brown',
                                                'id': '588ecfe9-05b0-40c2-aa32-3f26ee0b5281',
                                                'name': '★★★★'},
                                               {'color': 'purple',
                                                'id': '8e6092bc-4dcf-42fc-ae54-5e6bf1586c48',
                                                'name': '★★★'},
                                               {'color': 'orange',
                                                'id': 'ed26ef07-df97-4ced-9275-9aa0c71d5bb1',
                                                'name': '★★'},
                                               {'color': 'gray',
                                                'id': '632758dd-d74c-455c-8a3b-0d063282a3a7',
                                                'name': '★'}]},
                                    'type': 'select'}, ...

Refer above structure and fill in the code.
Write code with 'property' of 'select' from like follow as code.

select_property = {'평점': {'name': '평점',
                   'select': {'options': [{'color': 'green', 'name': '★★★'},
                                          {'color': 'purple', 'name': '★★'},
                                          {'color': 'gray', 'name': '★'}]
                             },
                   'type': 'select'}
                   }

We can add one property but try to add one more.
I bring the example with 'rich_text' form.

'코멘트': {'id': '%5C%3D%7B%5E',
         'name': '코멘트', # '코멘트' is a comment
         'rich_text': {},
         'type': 'rich_text'},

When we want to '메모'(memo) of property from the above property set to 'rich_text' form, write down code as follows.

rich_text_property = {'메모': {'name': '메모',
                              'type': 'rich_text',
                              'rich_text': {}
                              }
                      }

Now call 'create'... but before creating that, we skipped something.
property has key value. The key value is title. If we skip setting title, notion_client.errors.APIResponseError: Title is not provided will occur.

'맛집명': {'id': 'title',
         'name': '맛집명',
         'title': {},
         'type': 'title'}

Then like as above form, input the title_property variable.

title_property = {'맛집명': {'id': 'title', # '맛집명' means the tasty restaurant name
                           'name': '맛집명',
                           'title': {},
                           'type': 'title'
                           }
                 }

properties type is JSON, so bind as dictionary form.

properties_value = {}
properties_value.update(select_property)
properties_value.update(rich_text_property)
properties_value.update(title_property)

Now create and make a database.

notion.databases.create(parent=parent_id, title=title_value, icon=icon_value, properties=properties_value)

Is it possible to make a database on the uppermost?

When we want to make something on the uppermost, the parameter value is parent = {'type': 'workspace', 'workspace': True}. (Refer to the document Workspace parent)
But unfortunately, body failed validation: body.parent.page_id should be defined, instead was 'undefined' error occurs. For now, it seem to be impossible to make a database from the uppermost(workspace).

profile
QA Specialist

0개의 댓글