lastorel / pytion

Unofficial Python client for official Notion API
GNU General Public License v3.0
44 stars 3 forks source link

How do I append row to database? #63

Closed egeres closed 1 year ago

egeres commented 1 year ago

Accessing databases is pretty straightforward:

...
no   = Notion(token)
db   = no.databases.get(id)

And if we run db.db_query().obj.array, we can iterate over the entries in a notion database as well as see the properties of each object accessing .properties retrieves a dictionary with all the info... but I can't find a way to insert a new page in the database? I would have expected the following code to work, but it doesn't:

new_page = Page.create(
    parent = LinkTo(from_object=db.obj),
    properties = {
        "Name": PropertyValue.create("title", "My title")
    }
)
created_page = db.page_create(new_page)
lastorel commented 1 year ago

Hello, yes I can see that you execute page_create on db object. It works only with no.pages. at the moment. In your case:

new_page = Page.create(
    parent = LinkTo(from_object=db.obj),
    properties = {
        "Name": PropertyValue.create("title", "My title")
    }
)
created_page = no.pages.page_create(new_page)

The Page object already has parent attr, because it's mandatory. And there is a constrain to use page_create on specific Element objects. Otherwise it would be possible to execute db.page_create(new_page) and db2.page_create(new_page) that creates two identical pages in the same database Another way to append to database described here: https://github.com/lastorel/pytion/discussions/62

lastorel commented 1 year ago

Converting to discussion