Generating Models (flask make:model)¶
The flask make:model command seeds a SQLAlchemy model with a small, useful
starter shape: timestamps, helpers, and a registered import in
app/models/__init__.py.
Basic Example¶
flask make:model Post
This generates:
app/models/post.pywithPostboilerplatean import entry in
app/models/__init__.py
If the __init__.py file is missing, the model still gets created, and you
will see a warning so you can register the import manually.
Model Contents¶
The file includes:
id,created_at, andupdated_atcolumnsstore_in_databaseanddelete_from_databasehelpersa simple
__repr__for debugging
CRUD Scaffolding (Optional)¶
Add --crud to generate a matching controller, routes, and views for the
seven RESTful actions.
flask make:model Comment --crud
This uses the pluralized model name for the route group (comments) and
creates:
app/controllers/comment_controller.pyapp/routes/comments/withroutes.pyand__init__.pyapp/templates/comments/with views for the GET actions
The generated routes cover:
index(GET)show(GET)create(GET)store(POST)edit(GET)update(POST)destroy(POST)
Wrap-up¶
Use flask make:model when you want a clean model scaffold, and add
--crud when you want the full controller + routes + views wiring in one go.