
Reference API URL : Query a database
The database is bunches of a page.
We can bring the data through the query we want. First, get database_id via search and do retrieve.
databases = notion.search(filter={"property": "object", "value": "database"})
pprint(databases)
The result will show below if the API bot has the right to the database.
(If the result doesn't exist, We didn't share BOT in a database.)
{'has_more': False,
'next_cursor': None,
'object': 'list',
'page_or_database': {},
'results': [{'archived': False,
...,
'id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
Here database information exists in the 'results'.
One of the info the id is database_id.
Likewise, if we want to bring id of the first database, then bring the databases['results'][0]['id'] value, and want to get the second id, bring the databases['results'][1]['id'] value. Above all, we try to bring the id of the database value.
database_id = databases['results'][0]['id']
Now we got database_id so let's find out the query command. Code will be databases.query and look into the parameter.
filter value.
databases['results'][0]['properties'] then we can find the solution.{'상태': {'id': '%3EQQn',
'name': '상태',
'select': {'options': [{'color': 'green',
'id': '6da478a3-5307-4d77-a47a-934a86ddda48',
'name': '★★★★★'},
{'color': 'brown',
'id': '588ecfe9-05b0-40c2-aa32-3f26ee0b5281',
'name': '★★★★'},
...
'type': 'select'},
'이름': {'id': 'title',
'name': '이름',
'title': {},
'type': 'title'}}
The type of '상태'(condition) is 'select', and the type of '이름'(name) is 'title'. Add the data which contains '피자'(pizza).
condition = []
condition.append({'property': '이름',
'title': {'contains' : '피자'}
})
How I know it is 'contains', Text filter condition described it detailedly. Data that includes the data is 'contains', so write as JSON with this.
Then we will write JSON code '상태'(condition) is '★★★★'.
Note if the type is 'select', it doesn't have the 'contains' option and only exists four options. 'equals', 'does_not_equal', 'is_empty', 'is_not_empty'
condition.append({'property': '상태',
'select': {'equals' : '★★★★'}
})
And call with the 'and' option, and then the result will show. Call with' or' if you search for the 'or' option.
notion.databases.query(database_id=database_id, filter={'and': condition})
list type.sort_option = []
sort_option.append({'property': '상태', 'direction': 'ascending'})
sort_option.append({'property': '이름', 'direction': 'descending'})
pprint(notion.databases.query(database_id=database_id, sorts=sort_option, page_size=5))
I already mentioned start_cursor and page_size from 08, 09 Analysis - Search series, but I wrote them down because I'm kind.
start_cursor (string)
If supplied, this endpoint will return a page of results starting after the cursor provided. If not supplied, this endpoint will return the first page of results.
Maximum is 100 bunches from the search method. If the result exceeds 100 bunches, the response will give "has_more" : True and "next_cursor": "{unique id}". Refer next_cursor value and start_cursor, and then you can search after the 101st result.
page_size (int32)
The number of items from the full list desired in the response. Maximum: 100
You can set to bring how many bunches at once. If you skip it, it will get 100 bunches as the default value.