From d345b5bbbfddde50ca58a19e70fd7d646fc6ba50 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 15 Oct 2024 18:49:24 +0300 Subject: [PATCH 1/2] docs: mention `id` field in `1-abstract-dto.rst` --- .../examples/data_transfer_objects/factory/marking_fields.py | 5 +++++ .../factory/simple_dto_factory_example.py | 2 ++ docs/usage/dto/1-abstract-dto.rst | 2 ++ 3 files changed, 9 insertions(+) diff --git a/docs/examples/data_transfer_objects/factory/marking_fields.py b/docs/examples/data_transfer_objects/factory/marking_fields.py index 7649f2f8b5..0574273c3f 100644 --- a/docs/examples/data_transfer_objects/factory/marking_fields.py +++ b/docs/examples/data_transfer_objects/factory/marking_fields.py @@ -10,6 +10,8 @@ class User(Base): + # `Base` defines `id` field as: + # id: Mapped[UUID] = mapped_column(default=uuid4, primary_key=True) name: Mapped[str] password: Mapped[str] = mapped_column(info=dto_field("private")) created_at: Mapped[datetime] = mapped_column(info=dto_field("read-only")) @@ -20,6 +22,9 @@ class User(Base): @post("/users", dto=UserDTO, sync_to_thread=False) def create_user(data: User) -> User: + # even though the client did not send the id field, + # since it is a primary key it is autogenerated + assert "id" in vars(data) # even though the client sent the password and created_at field, it is not in the data object assert "password" not in vars(data) assert "created_at" not in vars(data) diff --git a/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py b/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py index 0bcf7fd184..88f78beb16 100644 --- a/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py +++ b/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py @@ -9,6 +9,8 @@ class User(Base): + # `Base` defines `id` field as: + # id: Mapped[UUID] = mapped_column(default=uuid4, primary_key=True) name: Mapped[str] password: Mapped[str] created_at: Mapped[datetime] diff --git a/docs/usage/dto/1-abstract-dto.rst b/docs/usage/dto/1-abstract-dto.rst index 5c8d8995d9..de8ce1df08 100644 --- a/docs/usage/dto/1-abstract-dto.rst +++ b/docs/usage/dto/1-abstract-dto.rst @@ -50,6 +50,8 @@ Fields marked as ``"private"`` or ``"read-only"`` will not be parsed from client :emphasize-lines: 6,14,15 :linenos: +Note that ``id`` field is handled specially, since it is a primary key. + .. note: The procedure for "marking" a model field will vary depending on the library. For example, From 4edd216df194121ae80c662d89f5ad51fc9b301f Mon Sep 17 00:00:00 2001 From: Cody Fincher <204685+cofin@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:18:15 -0600 Subject: [PATCH 2/2] Update docs/usage/dto/1-abstract-dto.rst --- docs/usage/dto/1-abstract-dto.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/dto/1-abstract-dto.rst b/docs/usage/dto/1-abstract-dto.rst index de8ce1df08..a39f583ef2 100644 --- a/docs/usage/dto/1-abstract-dto.rst +++ b/docs/usage/dto/1-abstract-dto.rst @@ -50,7 +50,7 @@ Fields marked as ``"private"`` or ``"read-only"`` will not be parsed from client :emphasize-lines: 6,14,15 :linenos: -Note that ``id`` field is handled specially, since it is a primary key. +Note that ``id`` field is the primary key and is handled specially by the defined SQLAlchemy base. .. note: