anvil.tables Module
Globals
app_tables
Classes
Row
A table row
Instance Methods
delete()
Delete the row from its data table
get_id() → id
Get the unique ID of the table row
update(**column_values)
update the data for multiple columns
SearchIterator
An iterator of table rows returned from a search()
Instance Methods
delete_all_rows()
Delete all the rows matching this search from the data table.
Only available with Accelerated Data Tables; not available with legacy Data Tables.
to_csv([escape_for_excel=False]) → Media object
Get the results of the SearchIterator in CSV format, optionally escaped for use in Excel. Returns a downloadable Media object; use its url property.
Table
A table returned from app_tables
Instance Methods
add_row(**column_values) → table row
Add a row to the data table. Use keyword arguments to specify column values.
client_readable() → client readable view
Return a view on the table that can be read by client code. Use keyword arguments to specify view restrictions
client_writable() → client writable view
Return a view on the table that can be written by client code. Use keyword arguments to specify view restrictions. This does not give the client write access to other tables referred to by the table.
client_writable_cascade() → client writable view
Return a view on this table that can be written by client code. Use keyword arguments to specify view restrictions.
delete_all_rows()
Delete all the rows from the data table
get()
Get a single matching row from the data table whose columns match the keyword arguments. Returns None if no matching row exists, and raises an exception if more than one row matches.
Eg: app_tables.table_1.get(name=‘John Smith’)
get_by_id(id) → row
Get the matching row from this data table, by its unique ID
has_row(row) → bool
Returns true if the table (or view) contains the provided row.
list_columns() → list of dicts
Get the spec for the table as a list of dicts. Each dict contains the name and type of a column.
search() → Row or None
Get rows from a data table. If you specify keyword arguments, you will retrieve only rows whose columns match those values.
Eg: app_tables.table_1.search(name=‘John Smith’)
to_csv([escape_for_excel=False]) → Media object
Get the table in CSV format, optionally escaped for use in Excel. Returns a downloadable Media object; use its url property.
Transaction
Create a new ‘Transaction’ object
Constructor
Transaction()
Instance Methods
__enter__() → anvil.tables.Transaction instance
Begin the transaction. If a transaction is already open, this block is merged into it: it commits or rolls back with the outermost transaction.
__exit__()
End the transaction
abort()
Abort the transaction this block is part of. When the outermost transaction ends, all write operations performed during it will be cancelled. Raises an error if this block is nested in a transaction opened by a different server call (for example, the server function that called this one), or if this transaction is not open.
Functions
get_connection_string([via_host=], [via_port=])
Get a Postgres connection string for accessing this app’s Data Tables via SQL.
The returned string includes temporary login credentials and sets the search path to a schema representing this app’s Data Table environment.
You can override the host and port for the database connection to connect via a secure tunnel.
(Available on the Dedicated Plan only.)
get_table_by_id(table_id) → anvil.tables.Table
(more info)
Get a table by id. Can pass a row id in, and the table the row belongs to will be returned.
in_transaction(function, server_function)
(more info)
When applied to a function (as a decorator), the whole function will run in a data tables transaction. If it conflicts with another transaction, it will retry up to five times.
on_transaction_end([before_commit=], [after_commit=], [after_rollback=])
(more info)
Register callbacks to run when the current data tables transaction ends. before_commit callbacks run (with no arguments) just before the transaction attempts to commit; after_commit callbacks run (with no arguments) after it commits successfully; after_rollback callbacks run after the transaction is rolled back, receiving the exception that caused the rollback (or None for an explicit abort()). If no transaction is open, before_commit and then after_commit are called immediately.
order_by(column_name, ascending=)
Sort the results of this table search by a particular column. Default to ascending order.
Globals
app_tables
(more info)
Access Table objects from the datatables services. You can access a Table object with dot notation e.g. app_tables.my_table. To access a table with strings use getattr(app_tables, 'my_table'). If no table is present an AttributeError will be thrown.
Exceptions
NoSuchColumnError
Raised when attempting to access a column that does not exist in this table.
QuotaExceededError
Raised when an app has exceeded its quota.
RowDeleted
Raised when attempting to accessing a table row that has been deleted - for example, accessing a row after calling its delete() method, or following a link to a deleted row.
TableError
Superclass of all table exceptions
TransactionConflict
Raised when a transaction conflicts and has been aborted.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.