Skip to main content

Module model_admin

Module model_admin 

Source
Available on (crate features model-mysql or model-postgres or model-sqlite) and non-WebAssembly only.
Expand description

Model admin (18.35.0, #109): list, search, show, create, edit and delete rows of the tables you register, as server-rendered pages — Django’s admin, for a DbPool (model-* features). A middleware, so it works in front of sync and async apps alike.

use rust_web_server::app::App;
use rust_web_server::model_admin::ModelAdmin;

let admin = ModelAdmin::new(pool.clone())
    .model::<Post>()                      // or .table("posts")
    .allow(|request| is_admin(request));     // e.g. auth_flows::CurrentUser::of(request) is an admin
// Inside the layers it needs (the first wrapped is the outermost):
// FlashLayer, CsrfLayer, AuthFlows, then the admin. Pages under /admin.
let app = App::with_async_state(pool).wrap(admin);

Nobody gets in until allow says so: without it every page answers 403. Wrap it after your sign-in (AuthFlows), FlashLayer and CsrfLayer — layers wrapped first are outside it and see its pages; the forms carry the CSRF field when that layer is there.

The columns, their types and the primary key come from the database (DbPool::schema), read when a page is drawn, so a migration shows at once. Form values are converted by the column’s type (integers, floats, booleans as a checkbox, text; an empty field is NULL for a nullable column); created_at/updated_at columns are set to now when the form leaves them empty. What the database refuses (a NOT NULL, a unique index, a foreign key) comes back as an alert on the form. Tables need a one-column primary key to be edited; others are listed only.

Structs§

ModelAdmin
The admin pages. See the module docs.

Constants§

PAGE_SIZE
Rows per page.