13

I have some Python code that retrieves the rows in a Notion database using the notion-client library. The code does manage to retrieve all the rows, but the order is wrong. I looked at the Sort object from the API reference, but was unable to figure out how to use it to return the rows in the exact order in which they're displayed on notion.so. Here's the snippet in question:

from notion_client import Client

notion = Client(auth=NOTION_API_TOKEN)
result = notion.databases.query(database_id='...')
for row in result['results']:
  title = row['properties']['NAME_OF_PROPERTY']['title']
  if len(title) == 0:
    print('')
  else:
    print(title[0]['plain_text'])

What am I missing?

feihong
  • 131
  • 1
  • 4

3 Answers3

3

The Notion API does not support views in the current version, so it is not necessarily going to match the order you have it in unless you have applied a sort or filter that you can also apply via the API.

adlopez15
  • 3,449
  • 2
  • 14
  • 19
  • I may be mistaken, but it wouldn't surprise me if Notion does not provide allow one to preserver the presentation sort order on purpose. They would like you to use their API to move data into their system, but would prefer if you didn't pull data out. – jdg May 04 '22 at 01:30
0

This is working as well as their documentation

const response = await notion.databases.query({
    database_id: databaseId,
    filter: {
      or: [
        {
          property: 'In stock',
          checkbox: {
            equals: true,
          },
        },
        {
          property: 'Cost of next trip',
          number: {
            greater_than_or_equal_to: 2,
          },
        },
      ],
    },
    sorts: [
      {
        property: 'Last ordered',
        direction: 'ascending',
      },
    ],
  });
Andy
  • 39
  • 1
-1

Use the order argument to notion.databases.query(). This argument is a list of sort specifications, which are dictionaries.

result = notion.databases.query(
    database_id = 'df4dfb3f-f36f-462d-ad6e-1ef29f1867eb',
    sort = [{"property": "NAME_OF_PROPERTY", "direction": "ascending"}]
)

You can put multiple sort specifications in the list, the later ones will be used for rows that are equal in the preceding properties.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Yes, I tried this, but what do I put for "property"? I don't want to sort by any particular property (column), I just want to sort by the order in which they appear in the table. In my case, it's not practical for me to add another column to the table that corresponds to the display order. – feihong Jun 06 '21 at 01:45
  • 1
    In relational databases, tables aren't ordered. If you want the results in a particular order, you need to specify the ordering. – Barmar Jun 06 '21 at 01:58
  • 3
    @Barmar well in general for relational databases, that's true, yet in Notion views, (if there's no sorting set up) you can manually drag items around for custom ordering. I guess that's stored internally with the view somewhere, and querying views – according to `adlopez15`'s answer – isn't currently possible due to API limitations. – v01pe Jun 11 '21 at 09:03