Skip to content

Commit 91d3932

Browse files
authored
refactor docs (#1301)
1 parent f564a2c commit 91d3932

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ A Simple Example
4040
db = SQLAlchemy(app, model_class=Base)
4141
4242
class User(db.Model):
43-
id: Mapped[int] = mapped_column(db.Integer, primary_key=True)
44-
username: Mapped[str] = mapped_column(db.String, unique=True, nullable=False)
43+
id: Mapped[int] = mapped_column(primary_key=True)
44+
username: Mapped[str] = mapped_column(unique=True)
4545
4646
with app.app_context():
4747
db.create_all()

docs/customizing.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ joined-table inheritance.
3737
db = SQLAlchemy(app, model_class=Base)
3838
3939
class User(db.Model):
40-
name: Mapped[str] = mapped_column(String)
40+
name: Mapped[str]
4141
4242
class Employee(User):
43-
title: Mapped[str] = mapped_column(String)
43+
title: Mapped[str]
4444
4545
4646
Abstract Models and Mixins
@@ -52,34 +52,33 @@ they are created or updated.
5252

5353
.. code-block:: python
5454
55-
from datetime import datetime
56-
from sqlalchemy import DateTime, Integer, String
57-
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, declared_attr
55+
from datetime import datetime, timezone
56+
from sqlalchemy.orm import Mapped, mapped_column
5857
5958
class TimestampModel(db.Model):
6059
__abstract__ = True
61-
created: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
62-
updated: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
60+
created: Mapped[datetime] = mapped_column(default=lambda: datetime.now(timezone.utc))
61+
updated: Mapped[datetime] = mapped_column(default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
6362
6463
class Author(db.Model):
65-
id: Mapped[int] = mapped_column(Integer, primary_key=True)
66-
username: Mapped[str] = mapped_column(String, unique=True, nullable=False)
64+
id: Mapped[int] = mapped_column(primary_key=True)
65+
username: Mapped[str] = mapped_column(unique=True)
6766
6867
class Post(TimestampModel):
69-
id: Mapped[int] = mapped_column(Integer, primary_key=True)
70-
title: Mapped[str] = mapped_column(String, nullable=False)
68+
id: Mapped[int] = mapped_column(primary_key=True)
69+
title: Mapped[str]
7170
7271
This can also be done with a mixin class, inheriting from ``db.Model`` separately.
7372

7473
.. code-block:: python
7574
7675
class TimestampMixin:
77-
created: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
78-
updated: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
76+
created: Mapped[datetime] = mapped_column(default=lambda: datetime.now(timezone.utc))
77+
updated: Mapped[datetime] = mapped_column(default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
7978
8079
class Post(TimestampMixin, db.Model):
81-
id: Mapped[int] = mapped_column(Integer, primary_key=True)
82-
title: Mapped[str] = mapped_column(String, nullable=False)
80+
id: Mapped[int] = mapped_column(primary_key=True)
81+
title: Mapped[str]
8382
8483
8584
Disabling Table Name Generation

docs/models.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ is not set and a primary key column is defined.
8686
from sqlalchemy.orm import Mapped, mapped_column
8787
8888
class User(db.Model):
89-
id: Mapped[int] = mapped_column(db.Integer, primary_key=True)
90-
username: Mapped[str] = mapped_column(db.String, unique=True, nullable=False)
91-
email: Mapped[str] = mapped_column(db.String)
89+
id: Mapped[int] = mapped_column(primary_key=True)
90+
username: Mapped[str] = mapped_column(unique=True)
91+
email: Mapped[str]
9292
9393
9494
Defining a model does not create it in the database. Use :meth:`~.SQLAlchemy.create_all`

docs/pagination.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following Jinja macro renders a simple pagination widget.
5858
.. code-block:: jinja
5959
6060
{% macro render_pagination(pagination, endpoint) %}
61-
<div class=page-items>
61+
<div class="page-items">
6262
{{ pagination.first }} - {{ pagination.last }} of {{ pagination.total }}
6363
</div>
6464
<div class=pagination>

docs/quickstart.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ The model will generate a table name by converting the ``CamelCase`` class name
115115
from sqlalchemy.orm import Mapped, mapped_column
116116
117117
class User(db.Model):
118-
id: Mapped[int] = mapped_column(Integer, primary_key=True)
119-
username: Mapped[str] = mapped_column(String, unique=True, nullable=False)
120-
email: Mapped[str] = mapped_column(String)
118+
id: Mapped[int] = mapped_column(primary_key=True)
119+
username: Mapped[str] = mapped_column(unique=True)
120+
email: Mapped[str]
121121
122122
123123
See :doc:`models` for more information about defining and creating models and tables.

0 commit comments

Comments
 (0)