Generating admin views (kloudmint scaffold)¶
kloudmint scaffold reads your SQLAlchemy models and writes a first-draft admin views.py: a view
class per model, parent/child wiring, search, soft delete and the lists you need to register and seed
them. It is the fastest way to start a new project's admin -- you then edit the result.
# from the project root
kloudmint scaffold --base app.models:Base --out app/admin # preview, writes nothing
kloudmint scaffold --base app.models:Base --out app/admin --write # write app/admin/views.py
--base is your declarative Base as module:Base. If your models live in other modules than the one
that defines Base, add --import app.more_models (repeatable) so they register on it.
Options¶
| Option | Meaning |
|---|---|
--base module:Base |
Your declarative Base (required) |
--out dir |
Directory for views.py (created, with an __init__.py, if new) |
--write |
Write the file. Without it you get a preview and nothing touches disk |
--models A B |
Only these models |
--exclude A B |
Skip these models (e.g. --exclude User to hand-write the user view) |
--import module |
Import an extra module so its models register on Base |
--include-sensitive |
Show password/token/secret-like columns instead of leaving them out |
Safety¶
- Never overwrites. If
views.pyalready exists the output goes toviews.generated.pynext to it for you to diff. - Never edits your other files. It prints what to add; you paste it.
- Sensitive columns are left out unless you pass
--include-sensitive. - Anything it can't decide safely is a
# TODOcomment in the generated class, never a silent guess.
What it decides for you¶
| It sees | It does |
|---|---|
A nullable datetime named deleted_at |
Adds SoftDeleteMixin; hides the column. Deleted rows disappear from the admin and Delete only stamps the column -- remove the mixin if admins need to see deleted rows |
A many-to-one relationship through one foreign key that cascades on delete (or is the reverse of a delete-orphan relationship) |
Makes the model a ChildOf its parent, and adds RelatedRecordsMixin + a Related(...) link with a count on the parent |
| Two foreign keys to the same parent | Sets parent_fk / parent_relationship_name for the owning one |
| A foreign key column | Shows the relationship (Certificate.candidate) instead of the raw id |
| A many-to-many relationship | A multi-select field |
String columns named like name, email, title, phone, slug, code, username, label, question, subject |
Added to the search list (up to 4) |
created_at |
Default sort, newest first |
id, timestamps |
In the list; timestamps out of the form |
| Long text / JSON columns | In the form, out of the list |
| More than 8 list columns | Keeps the 8 most useful (ids, relationships, search columns, status-like columns, created_at), shown in definition order; the rest are commented out |
| A password/hash, token, secret, api key, otp, pepper or salt column | Left out, with a comment |
The parent's display column is the first of name, full_name, title, email, username, slug,
label, question that exists (else its first text column, else id with a TODO).
What it leaves to you (as TODO comments)¶
- Ambiguous ownership -- a model owned by two parents (both cascading). Decide which one it is a
ChildOf. - File uploads -- a
*_key/*_pathcolumn next to a*_filenamecolumn is left out; seeFileUploadMixin+FileSpecin BuildingBlocks.md. - Passwords -- a model with a password column needs a password flow for creating users (RegisteringAdminViews.md, section 4).
- Binary columns (can't be edited in a form), self-referencing tables, and models with no ownership signal
(they get a plain view -- add
ChildOfyourself if it is a child). - Icons, categories and menu grouping, read-only views (
can_create = False), enum dropdowns and column formatters. The generated file doesn't set these.
kloudmint's own Role / Permission and the activity-log model are skipped (the report says why); write
those views by hand with the protection mixins (RoleAndPermissionManagement.md).
After generating¶
- Read every
# TODOand the report the command prints. - Register the views and grant their permissions:
from app.admin.views import GENERATED_VIEWS, RESOURCES
admin.register(*GENERATED_VIEWS) # parents first = sidebar order
# in your seed script: grant every action on each name in RESOURCES to your admin role
- Edit freely -- the file is yours. Re-running never touches it.
Example¶
# generated for Candidate (has deleted_at, children) and Certificate (cascades to Candidate)
class CertificateAdmin(ChildOf, RBACModelView, model=Certificate):
name_plural = "Certificates"
parent = Candidate
parent_display_field = "full_name"
column_list = [Certificate.id, Certificate.candidate, Certificate.name, Certificate.expiry_date]
column_searchable_list = [Certificate.name]
form_columns = [Certificate.candidate, Certificate.name, Certificate.expiry_date]
# TODO: file_filename / file_s3_key look like a file upload -- left out; see FileUploadMixin + FileSpec
class CandidateAdmin(SoftDeleteMixin, RelatedRecordsMixin, RBACModelView, model=Candidate):
related = [Related(CertificateAdmin, label="Certificates", count=True)]
...
Limits of this first version¶
It does not generate migrations, a main.py or seed script, formatters, enum dropdowns or upload specs.
It only looks at models registered on the Base you give it.