Choose what columns are available in a Data Table View

Here’s a new feature for Data Tables for which we’ve received a few requests:

You can now create column-restricted views in Accelerated Tables!

That’s right, you can create a view object that only exposes certain columns of your table, and pass it back to client code. This is great for exposing only certain data to client code.

To use it, pass q.only_cols("column_name", "other_column", ...) to your view function. So, for example, this server function allows client code to view only the email and enabled columns of the Users table, leaving all other data inaccessible:

@anvil.server.callable
def get_users():
    return app_tables.users.client_readable(q.only_cols("email", "enabled"))

This can of course be combined with other view constraints, to columns that are or are not visible to the view user. For example:

app_tables.users.client_readable(q.only_cols("email"), enabled=True)

Go check out the documentation!

Column-restricted views (Data Tables documentation)


Particular props go out to @stucork for this one, with a few contributions from yours truly :wink:

13 Likes

Does this work with linked loads and nested linked loads?

I want to preface the following comments by saying this is a terrific feature, and will improve things in a lot of situations, and I’m super pleased to see it arrive.

Are there any plans to make the same mechanism work in other areas where we might want to limit columns? e.g.

With non-view search results? e.g. app_tables.users.search(q.only_cols('email'))

With results from anvil.users.get_user(q.only_cols('email'))?

Linked fields have already been mentioned

2 Likes

I’ll say in general that we’re thinking a lot about Data Tables, and how to make it easier to use them – and use them securely – without having to pipe every operation through a server module.

In the meantime, you can already accomplish both use cases you’re asking about!

For search results:

app_tables.users.client_readable(q.only_cols("email")).search()

and narrowing a user row in particular:

user = anvil.users.get_user()
app_table.users.client_readable(q.only_cols("email")).get_by_id(user.get_id())
1 Like