F
FasORM
v0.1.0

Step 2: Defining Domain Models

Now we will write our FasORM model classes inside app/models/blog.py, declaring field validation and wiring bidirectional relationships.

Model Implementations

app/models/blog.py
from fasorm import (
    Model, String, Text, Enum,
    HasMany, BelongsTo
)

class User(Model):
    name = String(100)
    email = String(255, unique=True)
    password = String(255)
    
    # User has many posts and comments
    posts = HasMany("Post")
    comments = HasMany("Comment")

class Post(Model):
    title = String(200)
    body = Text()
    status = Enum(
        options=["draft", "published", "archived", "in-review"],
        default="draft"
    )
    
    # Relationships
    user = BelongsTo("User")      # Author
    comments = HasMany("Comment")  # Related comments

class Comment(Model):
    body = Text()
    
    # Relationships
    post = BelongsTo("Post")  # Target post
    user = BelongsTo("User")  # Comment author

Model Methods & Capabilities

Status Enum Enforcement

The status field restricts assigned values to "draft", "published", "archived", or "in-review".

Bidirectional Eager Loading

Allows loading posts with comments: Post.with_("comments", "user") or loading users with posts.

Relation Count Aggregation

Instantly fetch comment counts without loading full comment objects: Post.with_count("comments").

← Step 1: Setup Step 3: FastAPI Routes →