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
