Skip to main content

Module chooser

Module chooser 

Source
Expand description

Generic chooser registry — register a chooser for any #[derive(Model)] type, generalising the three hand-rolled choosers (page / snippet / document).

A host registers with register_chooser!(Ty, "slug", "Label", "title_field", "sub_field"); the admin then serves /cms-admin/__chooser/<slug>?q=… returning the same { "items": [ { id, title, sub, … } ] } shape the bespoke choosers use. The editor’s chooser overlay (cms-ux.js) resolves an unregistered data-chooser-kind to this endpoint automatically, so a registered chooser needs no JavaScript change — a widget just sets its slug as the kind.

Search runs over the model’s admin(search_fields = …) (the same metadata the framework admin uses), matched case-insensitively as a substring on each declared field — mirroring the in-memory filter the bespoke choosers already do. A dedicated form WidgetKind for model choosers is the natural next step.

Structs§

ChooserViewSet
One registered chooser.

Constants§

CHOOSER_LIMIT
Cap on returned rows — matches the bespoke choosers’ limit.

Functions§

filter_and_normalize
Filter already-serialized rows by query (case-insensitive substring over search_fields), cap at limit, and normalise each to carry a title + sub key (from title_field / sub_field) alongside its original fields. An empty / blank query returns the first limit rows. Pure — the register_chooser! thunk and the unit tests both call it.
find
The chooser registered under slug, if any.
registered
Every registered chooser, sorted by slug.

Type Aliases§

SearchRowsFn
Type-erased “search rows as normalised chooser items”. Built by register_chooser! from the concrete model’s objects().fetch() + its schema search_fields (the fetch is a derive-generated inherent method, so it can’t be reached generically — hence the thunk, same shape as crate::admin::model_admin).