REST API CRUD Generator

Lila can generate a full async REST endpoint set for any ActiveRecord model with one call.

Router.rest_crud_generate() mounts list/get/create/update/delete routes that use the model's async CRUD methods. It does not require a Pydantic schema and does not generate HTML — for an HTML DataTable + Pydantic scaffold use lila crud (see the Scaffold CRUD page).

Create the table first — lila migrate creates missing tables and adds missing columns:

term

lila migrate

# Destructive reset (asks for confirmation without --yes):
lila migrate --refresh --yes


app/routes/api/users.py

from lila.core.routing import Router
from app.models.user import User

router = Router(prefix="/api")

# Mounts /users and /users/{id} using User.get_all_async / insert_async / ...
# base_route defaults to the model's __tablename__ ("users").
router.rest_crud_generate(User, base_route="users", auth=True)
                            

auth=True requires an active session for every generated endpoint: API paths get 401, pages get a 303 redirect to /login. The generated routes are cached per identity like any other GET route.

Function signature
lila/core/routing.py

def rest_crud_generate(
    self,
    model_sql,                 # BaseModel subclass
    base_route: str | None = None,   # defaults to model_sql.__tablename__
    auth: bool = False,              # require a session on every route
    max_limit: int = 500,            # upper bound for ?limit= on the list route
): ...
# POST/PUT only accept real table columns: the primary key, __private_fields__
# and unknown keys are rejected with 422. Database errors are logged, never returned.
                    
Generated endpoints

GET    /api/users              → await User.get_all_async(limit=100, offset=0)  (limit clamped to max_limit)
GET    /api/users/{id}         → await User.get_by_id_async(id)   (404 if missing)
POST   /api/users              → await User.insert_async(**json_body)      (201)
PUT    /api/users/{id}         → await User.update_async(id, **json_body)  (404 if missing)
DELETE /api/users/{id}         → await User.delete_async(id)               (404 if missing)
            

POST expects a JSON object whose keys match the model columns; PUT accepts a partial JSON object. DELETE is a soft delete when the model has an active column. There is no Pydantic validation or password hashing in this generic generator: if you need it, use the lila crud scaffold (Pydantic models + CSRF) or the Admin 2.0 CRUD (sanitized records + password hashing).

Automatic documentation

Routes registered on a router appear in the app's schema. With App(openapi=True) (the default) the docs are at http://127.0.0.1:8000/docs and the schema at /openapi.json. Set Router(openapi=False) (as the admin does) to hide routes.

Swagger UI — GET list
Swagger UI GET list example
JSON response
Swagger UI JSON response example
POST — create
Swagger UI POST example
GET by ID
Swagger UI GET by ID example
PUT — update
Swagger UI PUT example
DELETE
Swagger UI DELETE example

In this example the resource is users; the same call works for products, stores, etc. by passing a different model and base_route.


HTML CRUD

rest_crud_generate only creates JSON endpoints. For a generated HTML page with a responsive DataTable, create/edit modals, Pydantic validation and CSRF, run:


lila crud --model Product
            

That command writes app/routes/web/product.py (page), app/routes/api/product.py (Pydantic API) and resources/html/product/index.jinja (DataTable). See the Scaffold CRUD page for details.