TableConfig¶
When using create_admin
, you can pass in normal Table
classes:
from piccolo_admin.endpoints import create_admin
create_admin([Director, Movie])
Alternatively, you can pass in TableConfig
instances (or mix and match
them).
By passing in a TableConfig
you have extra control over how the UI behaves
for that table. This is particularly useful when you have a Table
with lots
of columns.
In the future, TableConfig
will be extended to allow finer grained control
over the UI.
visible_columns¶
We can set which columns are visible in the list view:
from piccolo_admin.endpoints import TableConfig
movie_config = TableConfig(Movie, visible_columns=[Movie.id, Movie.name])
create_admin([Director, movie_config])
Here is the UI when just passing in a Table
:
Here is the UI when just passing in a TableConfig
instance instead (fewer
columns are visible):
visible_filters¶
We can set which columns are visible in the filter sidebar:
from piccolo_admin.endpoints import TableConfig
movie_config = TableConfig(
Movie,
visible_filters=[
Movie.name, Movie.rating, Movie.director,
]
)
create_admin([Director, movie_config])
Here is the UI when just passing in a Table
:
Here is the UI when just passing in a TableConfig
instance instead (fewer
filters are visible in the sidebar):
rich_text_columns¶
We can specify which Text
columns will use a rich text editor.
from piccolo_admin.endpoints import TableConfig
movie_config = TableConfig(
Movie,
rich_text_columns=[
Movie.description
]
)
create_admin([Director, movie_config])
This allows the user to add hyperlinks, and basic formatting to their content, without having to write HTML.
hooks¶
Can be used to run custom logic when a row is created, modified, or deleted.
from piccolo_admin.endpoints import TableConfig
from piccolo_api.crud.hooks import Hook, HookType
async def my_save_hook(row: Movie):
# Insert custom logic here
return row
movie_config = TableConfig(
Movie,
hooks=[
Hook(hook_type=HookType.pre_save, callable=my_save_hook)
]
)
create_admin([Director, movie_config])
To learn more about hooks, see the Hook
docs in Piccolo API.
media_storage¶
Allows you store files (video / media / audio etc) in certain columns. See Media Storage.
validators¶
Allows fine grained access control over each API endpoint. See
TableConfig
for more
information.
link_column¶
We use the primary key as the link to the edit page, but we can specify
an alternative column to use as a link. If we specify the foreign key column as
link Piccolo Admin will raise ValueError
:
from piccolo_admin.endpoints import TableConfig
movie_config = TableConfig(
Movie,
link_column=Movie.name
)
create_admin([movie_config])
order_by¶
By default, the primary key is used to order the results, but we can specify one or more columns to order by instead.
Here we use the rating
column in descending order:
from piccolo_admin.endpoints import TableConfig, OrderBy
movie_config = TableConfig(
Movie,
order_by=[OrderBy(Movie.rating, ascending=False)]
)
create_admin([movie_config])
Here we order by the rating
and title
columns:
movie_config = TableConfig(
Movie,
order_by=[
OrderBy(Movie.rating, ascending=False),
OrderBy(Movie.title, ascending=True)
]
)
This means that the results are first ordered by rating
, and any rows with
an equal rating are then sorted by title
.
Note you can still override the order in the UI.
Source¶
- class piccolo_admin.endpoints.TableConfig(table_class: Type[Table], visible_columns: List[Column] | None = None, exclude_visible_columns: List[Column] | None = None, visible_filters: List[Column] | None = None, exclude_visible_filters: List[Column] | None = None, rich_text_columns: List[Column] | None = None, hooks: List[Hook] | None = None, media_storage: Sequence[MediaStorage] | None = None, validators: Validators | None = None, menu_group: str | None = None, link_column: Column | None = None, order_by: List[OrderBy] | None = None, time_resolution: Dict[Timestamp | Timestamptz | Time, float | int] | None = None)[source]¶
Gives the user more control over how a
Table
appears in the UI.- Parameters:
table_class – The
Table
class to configure.visible_columns – If specified, only these columns will be shown in the list view of the UI. This is useful when you have a lot of columns.
exclude_visible_columns – You can specify this instead of
visible_columns
, in which case all of theTable
columns except the ones specified will be shown in the list view.visible_filters – If specified, only these columns will be shown in the filter sidebar of the UI. This is useful when you have a lot of columns.
exclude_visible_filters – You can specify this instead of
visible_filters
, in which case all of theTable
columns except the ones specified will be shown in the filter sidebar.rich_text_columns – You can specify
rich_text_columns
if you want a WYSIWYG editor on certain PiccoloText
columns. Any columns not specified will use a standard HTML textarea tag in the UI.hooks – These are passed directly to
PiccoloCRUD
, which powers Piccolo Admin under the hood. It allows you to run custom logic when a row is modified.media_storage – These columns will be used to store media. We don’t directly store the media in the database, but instead store a string, which is a unique identifier, and can be used to retrieve a URL for accessing the file. Piccolo Admin automatically renders a file upload widget for each media column in the UI.
validators –
The
Validators
are passed directly toPiccoloCRUD
, which powers Piccolo Admin under the hood. It allows fine grained access control over each API endpoint. For example, limiting which users canPOST
data:from piccolo_api.crud.endpoints import PiccoloCRUD from starlette.exceptions import HTTPException from starlette.requests import Request async def manager_only( piccolo_crud: PiccoloCRUD, request: Request ): # The Piccolo `BaseUser` can be accessed from the request. user = request.user.user # Assuming we have another database table where we record # users with certain permissions. manager = await Manager.exists().where(manager.user == user) if not manager: # Raise a Starlette exception if we want to reject the # request. raise HTTPException( status_code=403, detail="Only managers are allowed to do this" ) admin = create_admin( tables=TableConfig( Movie, validators=Validators(post_single=[manager_only]) ) )
menu_group – If specified, tables can be divided into groups in the table menu. This is useful when you have many tables that you can organize into groups for better visibility.
link_column – In the list view of Piccolo Admin, we use the primary key to link to the edit page. However, if the primary key column is hidden, due to
visible_columns
orexclude_visible_columns
, then we need to specify an alternative column to use as the link.order_by – If specified, the rows are sorted by
order_by
, otherwise the defaultprimary_key
column is used to sort the rows.time_resolution –
Controls the resolution of
Time
columns, and the time component ofTimestamp
/Timestamptz
columns. The units are given in seconds. Some examples:0.001 - the max resolution is 1 millisecond (this is the minimum currently allowed by HTML input fields)
1 - the max resolution is 1 second (the default)
60 - the max resolution is 1 minute