The ORM

A small active-record ORM over sqlite — enough to build real apps, nothing to install.

class=kw>from larz.models class=kw>import Model, StrField, IntField, DateTimeField, ForeignKey, connect
connect("app.db")

class User(Model):
    name = StrField(unique=True); age = IntField(default=0)

class Post(Model):
    title = StrField(); views = IntField(default=0, index=True)
    created = DateTimeField(auto_now=True)
    author = ForeignKey(User)

User.create_table(); Post.create_table()      class=com># auto-migrates new columns

Fields

StrFieldTextFieldIntField FloatFieldBoolFieldDateTimeField DateFieldJSONFieldDecimalField ForeignKey

Create, read, update, delete

u = User.create(name="Ada", age=36)
p = Post.create(title="Hi", author=u)
Post.get(p.id)
p.update(views=5)
p.author.name              class=com># -> "Ada"  (foreign-key access)
p.delete()

Queries

Post.where(views__gt=100).all()
Post.where(title__like="Larz%").order("-created").all()
Post.where(views__in=[1, 2, 3]).count()
Post.where(author=u).page(2, per_page=20).all()
Post.where(published=True).exists()

Operators: __gt __gte __lt __lte __ne __in __like __contains.

Hooks & transactions

class Order(Model):
    total = IntField()
    class=kw>def before_save(self): ...
    class=kw>def after_save(self): ...

class=kw>from larz.models class=kw>import transaction
with transaction():           class=com># atomic; rolls back on exception
    a.update(bal=a.bal - 10)
    b.update(bal=b.bal + 10)
Get an auto-CRUD UI over any model with the admin panel. sqlite ships; the backend is pluggable for Postgres in production.