liveBook · Manning

Post

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Post is the book's SQLAlchemy model representing a blog post, its persisted attributes, and related comments.

Post gathers a blog entry’s content and metadata in one place, then persists it so the application can search, display, and manage it. Plain-text content can include Markdown for richer presentation. section 11.1

The SQLAlchemy model records the post’s identity, ownership, content, and lifecycle:

  • post_uid supplies a UUID primary key, while user_uid links each post to its author through a foreign key. section 11.1.1
  • The User.posts relationship exposes an author’s zero or more posts as a Python list, while each post can access its user. section 11.1.1

Creating a post

A registered user submits a validated PostForm. The handler creates a Post instance from the form content, adds it to a database session, commits it, reports success, and redirects to the new post. section 11.5.1

Post can also represent comments through a self-referential hierarchy. parent_uid identifies the parent, children exposes nested replies, and parent lets a child reference its parent. A generated sort_key preserves display order; because SQLite does not autoincrement nonprimary-key fields, get_next_sort_key() derives the next value from the current maximum. section 11.7.1

class Post(db.Model):
    __tablename__ = "post"                                           #1
    post_uid = db.Column(db.String, 
    primary_key=True, default=get_uuid)                            #2
    user_uid = db.Column(db.String, 
    db.ForeignKey("user.user_uid"), nullable=False, index=True)    #3
    title = db.Column(db.String)
    content = db.Column(db.String)
    active = db.Column(db.Boolean, nullable=False, default=True)
    created = db.Column(db.DateTime, 
    nullable=False, default=datetime.now(tz=timezone.utc))         #4
    updated = db.Column(db.DateTime, nullable=False, default=datetime.now(
        tz=timezone.utc), onupdate=datetime.now(tz=timezone.utc))    #5

Inspect the complete baseline Post model, including keys, author linkage, content fields, status, and timestamps. section 11.1.1

class Post(db.Model):
    __tablename__ = "post"
    post_uid = db.Column(db.String, primary_key=True, default=get_uuid)
    parent_uid = db.Column(db.String, 
    db.ForeignKey("post.post_uid"), 
    default=None)                               #1
    sort_key = db.Column(db.Integer, 
    nullable=False, unique=True, 
    default=get_next_sort_key)                  #2
    user_uid = db.Column(db.String, 
    db.ForeignKey("user.user_uid"), 
    nullable=False, index=True)
    title = db.Column(db.String)
    content = db.Column(db.String)
    children = db.relationship("Post", 
    backref=db.backref("parent", 
    remote_side=[post_uid], lazy="joined"))     #3
    active = db.Column(db.Boolean, nullable=False, default=True)
    created = db.Column(db.DateTime, 
    nullable=False, 
    default=datetime.now(tz=timezone.utc))
    updated = db.Column(db.DateTime, nullable=False, default=datetime.now(
        tz=timezone.utc), onupdate=datetime.now(tz=timezone.utc))

Inspect the expanded self-referential model used for parent posts, child comments, and ordered comment rendering. section 11.7.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Defines columns, relationships, creation workflow, and comment ordering, including SQLite’s limitation on nonprimary-key autoincrement.
Examples
Uses a baseline Post class, a validated creation handler, and nested comments ordered with generated sort keys.
What it assumes
Assumes familiarity with SQLAlchemy models, database sessions, foreign keys, relationships, and form validation.
What you can do after
Build it from scratch as a SQLAlchemy model, persist validated posts, and extend it with ordered parent-child comments.
How it connects
Links SQLAlchemy models with users, database tables, form validation, Markdown rendering, and self-referential comment relationships.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.