Data & Accounts
Model your data with the built-in ORM, add real user accounts with secure passwords, and issue API keys for a paid API.
The ORM
Define models; Larz creates and migrates the sqlite tables.
from larz.models import Model, StrField, IntField, DateTimeField, ForeignKey, connect
connect("app.db")
class Post(Model):
title = StrField(); views = IntField(default=0, index=True)
created = DateTimeField(auto_now=True)
author = ForeignKey("User")
Post.create_table()
Post.where(views__gt=100).order("-created").page(1).all()
post.author.name # foreign-key access
Accounts
import larz.auth as auth
auth.enable(app) # adds req.user + session auth
app.auth.register("a@b.com", "password") # scrypt-hashed
app.auth.login(req, "a@b.com", "password")
@app.login_required
@app.get("/me")
def me(req): return req.user.email
A logged-in user's entitlements follow their account, not their browser โ so Pro sticks across devices.
API keys
key = app.auth.issue_api_key(plan="pro") # shown once; only a hash is stored
@app.api_key_required(plan="pro")
@app.metered("$0.01/call")
@app.post("/api/x")
def x(req): ...
Takeaways
- The ORM has relationships, query operators, pagination, hooks and transactions.
- Passwords use scrypt; sessions are signed; tokens handle email-verify + reset.
- Metering an API-key request bills the key, so you can sell a public API.