From 4403133d1bab28fab0f5b0fac5e625ceffe33319 Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Tue, 29 Jul 2025 00:05:55 +0200 Subject: [PATCH 1/4] [IMP] pydantic: support to pydantic v2 and higher --- .github/workflows/test.yml | 14 +- pydantic/README.rst | 49 +++-- pydantic/readme/CONTRIBUTORS.md | 1 + pydantic/readme/DESCRIPTION.md | 5 +- pydantic/readme/USAGE.md | 28 ++- pydantic/static/description/index.html | 67 +++--- pydantic/tests/__init__.py | 1 + .../test_pydantic_generic_odoo_getter.py | 206 ++++++++++++++++++ pydantic/utils.py | 186 +++++++++++----- 9 files changed, 447 insertions(+), 110 deletions(-) create mode 100644 pydantic/tests/__init__.py create mode 100644 pydantic/tests/test_pydantic_generic_odoo_getter.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a06488079..0e8c681ab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,8 +38,11 @@ jobs: - container: ghcr.io/oca/oca-ci/py3.10-odoo18.0:latest name: test with Odoo - container: ghcr.io/oca/oca-ci/py3.10-ocb18.0:latest - name: test with OCB - makepot: "true" + name: test with OCB and pydantic > 2.0 + pydantic2: "true" + - container: ghcr.io/oca/oca-ci/py3.10-ocb18.0:latest + name: test with OCB and pydantic < 2.0 + exclude: "fastapi" services: postgres: image: postgres:12.0 @@ -51,6 +54,7 @@ jobs: - 5432:5432 env: OCA_ENABLE_CHECKLOG_ODOO: "1" + EXCLUDE: "${{ matrix.exclude }}" steps: - uses: actions/checkout@v4 with: @@ -63,6 +67,12 @@ jobs: run: manifestoo -d . check-dev-status --default-dev-status=Beta - name: Initialize test db run: oca_init_test_database + - name: Ensure pydantic version < 2 + run: pip install "pydantic<2.0" + if: ${{ matrix.pydantic2 != 'true' }} + - name: Ensure pydantic version > 2 + run: pip install "pydantic>2.0" + if: ${{ matrix.pydantic2 == 'true' }} - name: Run tests run: oca_run_tests - uses: codecov/codecov-action@v4 diff --git a/pydantic/README.rst b/pydantic/README.rst index cddfbb432..d8acaea95 100644 --- a/pydantic/README.rst +++ b/pydantic/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ======== Pydantic ======== @@ -17,7 +13,7 @@ Pydantic .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github @@ -33,13 +29,13 @@ Pydantic |badge1| |badge2| |badge3| |badge4| |badge5| This addon provides a utility method that can be used to map odoo record -to a `Pydantic model `__. +to a `Pydantic model `__. If you need to make your Pydantic models extendable at runtime, takes a look at the python package `extendable-pydantic `__ -and the odoo addon -`extendable `__ +and the `odoo addon +extendable `__ **Table of contents** @@ -51,9 +47,30 @@ Usage To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter. +fields from odoo models to fields defined by the pydantic model. + +To ease the mapping, the addon provide 2 utility classes: + +- Using ``pydantic>2.0``, + ``odoo.addons.pydantic.utils.PydanticOdooBaseModel``: + +.. code:: python + + from odoo.addons.pydantic.utils import PydanticOdooBaseModel + + + class Group(PydanticOdooBaseModel): + name: str + + class UserInfo(PydanticOdooBaseModel): + name: str + groups: List[Group] = pydantic.Field(alias="groups_id") + + user = self.env.user + user_info = UserInfo.from_orm(user) + +- Using ``pydantic<2.0``, + ``odoo.addons.pydantic.utils.GenericOdooGetter``: .. code:: python @@ -78,9 +95,8 @@ odoo.addons.pydantic.utils.GenericOdooGetter. user = self.env.user user_info = UserInfo.from_orm(user) -See the official `Pydantic -documentation `__ to discover all -the available functionalities. +See the official `Pydantic documentation `__ +to discover all the available functionalities. Known issues / Roadmap ====================== @@ -112,8 +128,9 @@ Authors Contributors ------------ -- Laurent Mignon -- Tris Doan +- Laurent Mignon +- Tris Doan +- Pierre Verkest Maintainers ----------- diff --git a/pydantic/readme/CONTRIBUTORS.md b/pydantic/readme/CONTRIBUTORS.md index 9b84ef6d3..29e261b02 100644 --- a/pydantic/readme/CONTRIBUTORS.md +++ b/pydantic/readme/CONTRIBUTORS.md @@ -1,2 +1,3 @@ - Laurent Mignon \<\> - Tris Doan \<\> +- Pierre Verkest \<\> diff --git a/pydantic/readme/DESCRIPTION.md b/pydantic/readme/DESCRIPTION.md index 83b7baff0..a00840439 100644 --- a/pydantic/readme/DESCRIPTION.md +++ b/pydantic/readme/DESCRIPTION.md @@ -1,8 +1,7 @@ This addon provides a utility method that can be used to map odoo record -to a [Pydantic model](https://pydantic-docs.helpmanual.io/). +to a [Pydantic model](https://docs.pydantic.dev/). If you need to make your Pydantic models extendable at runtime, takes a look at the python package [extendable-pydantic](https://pypi.org/project/extendable_pydantic/) and -the odoo addon -[extendable](https://github.com/acsone/odoo-addon-extendable) +the [odoo addon extendable](https://pypi.org/project/odoo-addon-extendable) diff --git a/pydantic/readme/USAGE.md b/pydantic/readme/USAGE.md index 92327bb6a..cfa4b6ce2 100644 --- a/pydantic/readme/USAGE.md +++ b/pydantic/readme/USAGE.md @@ -1,8 +1,28 @@ To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter. +fields from odoo models to fields defined by the pydantic model. + + +To ease the mapping, the addon provide 2 utility classes: + +* Using `pydantic>2.0`, `odoo.addons.pydantic.utils.PydanticOdooBaseModel`: + +``` python +from odoo.addons.pydantic.utils import PydanticOdooBaseModel + + +class Group(PydanticOdooBaseModel): + name: str + +class UserInfo(PydanticOdooBaseModel): + name: str + groups: List[Group] = pydantic.Field(alias="groups_id") + +user = self.env.user +user_info = UserInfo.from_orm(user) +``` + +* Using `pydantic<2.0`, `odoo.addons.pydantic.utils.GenericOdooGetter`: ``` python import pydantic @@ -28,5 +48,5 @@ user_info = UserInfo.from_orm(user) ``` See the official [Pydantic -documentation](https://pydantic-docs.helpmanual.io/) to discover all the +documentation](https://docs.pydantic.dev/) to discover all the available functionalities. diff --git a/pydantic/static/description/index.html b/pydantic/static/description/index.html index 557bfc29e..1b62e1f81 100644 --- a/pydantic/static/description/index.html +++ b/pydantic/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Pydantic -
+
+

Pydantic

- - -Odoo Community Association - -
-

Pydantic

-

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

+

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

This addon provides a utility method that can be used to map odoo record -to a Pydantic model.

+to a Pydantic model.

If you need to make your Pydantic models extendable at runtime, takes a look at the python package extendable-pydantic -and the odoo addon -extendable

+and the odoo addon +extendable

Table of contents

    @@ -397,12 +392,33 @@

    Pydantic

-

Usage

+

Usage

To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping -fields from odoo models to fields defined by the pydantic model. To ease -the mapping, the addon provide a utility class -odoo.addons.pydantic.utils.GenericOdooGetter.

+fields from odoo models to fields defined by the pydantic model.

+

To ease the mapping, the addon provide 2 utility classes:

+
    +
  • Using pydantic>2.0, +odoo.addons.pydantic.utils.PydanticOdooBaseModel:
  • +
+
+from odoo.addons.pydantic.utils import PydanticOdooBaseModel
+
+
+class Group(PydanticOdooBaseModel):
+    name: str
+
+class UserInfo(PydanticOdooBaseModel):
+    name: str
+    groups: List[Group] = pydantic.Field(alias="groups_id")
+
+user = self.env.user
+user_info = UserInfo.from_orm(user)
+
+
    +
  • Using pydantic<2.0, +odoo.addons.pydantic.utils.GenericOdooGetter:
  • +
 import pydantic
 from odoo.addons.pydantic import utils
@@ -425,12 +441,11 @@ 

Usage

user = self.env.user user_info = UserInfo.from_orm(user)
-

See the official Pydantic -documentation to discover all -the available functionalities.

+

See the official Pydantic documentation +to discover all the available functionalities.

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -446,22 +461,23 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • ACSONE SA/NV
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association @@ -476,6 +492,5 @@

Maintainers

-
diff --git a/pydantic/tests/__init__.py b/pydantic/tests/__init__.py new file mode 100644 index 000000000..5cf60de35 --- /dev/null +++ b/pydantic/tests/__init__.py @@ -0,0 +1 @@ +from . import test_pydantic_generic_odoo_getter diff --git a/pydantic/tests/test_pydantic_generic_odoo_getter.py b/pydantic/tests/test_pydantic_generic_odoo_getter.py new file mode 100644 index 000000000..0ca049486 --- /dev/null +++ b/pydantic/tests/test_pydantic_generic_odoo_getter.py @@ -0,0 +1,206 @@ +import datetime +from unittest import skipIf + +from odoo import fields +from odoo.tests import TransactionCase + +from pydantic import BaseModel, Field + +from ..utils import PYDANTIC_V2 + +if PYDANTIC_V2: + from ..utils import PydanticOdooBaseModel as PydanticOrmBaseModel + +else: + from ..utils import GenericOdooGetter + + class PydanticOrmBaseModel(BaseModel): + class Config: + orm_mode = True + getter_dict = GenericOdooGetter + + +class OdooBaseModel(PydanticOrmBaseModel): + id: int + + +class PartnerModel(OdooBaseModel): + name: str + date: datetime.date | None = None + + +class UserFlatModel(OdooBaseModel): + partner_id: int = Field(title="Partner") + + +class GroupModel(OdooBaseModel): + name: str + + +class UserModel(OdooBaseModel): + partner: PartnerModel = Field(title="Partner", alias="partner_id") + + +class UserDetailsModel(UserModel): + groups: list[GroupModel] = Field(alias="groups_id") + action_id: OdooBaseModel | None = None + signature: str | None = None + active: bool | None = None + share: bool | None = None + write_date: datetime.datetime + + +class CommonPydanticCase(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.user_demo = cls.env.ref("base.user_demo") + cls.user_demo.action_id = False + cls.user_demo.signature = False + cls.user_demo.share = False + + +@skipIf(PYDANTIC_V2, "Ignore because Pydantic >= 2.0 is installed") +class TestGenericOdooGetterPydanticV1Case(CommonPydanticCase): + def test_user_model_serialization(self): + self.user_demo.partner_id.date = None + self.assertEqual( + UserModel.from_orm(self.user_demo).dict(), + { + "id": self.user_demo.id, + "partner": { + "id": self.user_demo.partner_id.id, + "name": self.user_demo.partner_id.name, + "date": None, + }, + }, + ) + + def test_user_model_serialization_date(self): + self.user_demo.partner_id.date = fields.Date.today() + self.assertEqual( + UserModel.from_orm(self.user_demo).partner.date, + self.user_demo.partner_id.date, + ) + + def test_user_model_details_serialization_datetime(self): + user_demo = self.user_demo.with_context(tz="Asia/Tokyo") + self.assertEqual( + UserDetailsModel.from_orm(user_demo).write_date, + fields.Datetime.context_timestamp(user_demo, user_demo.write_date), + ) + self.assertNotEqual( + UserDetailsModel.from_orm(user_demo).write_date.tzinfo, + fields.Datetime.context_timestamp( + self.user_demo, user_demo.write_date + ).tzinfo, + ) + + def test_user_details_model_serialization(self): + self.assertEqual( + UserDetailsModel.from_orm(self.user_demo).dict(), + { + "id": self.user_demo.id, + "partner": { + "id": self.user_demo.partner_id.id, + "name": self.user_demo.partner_id.name, + "date": None, + }, + "groups": [ + { + "id": group.id, + "name": group.name, + } + for group in self.user_demo.groups_id + ], + "action_id": None, + "signature": None, + "active": True, + "share": False, + "write_date": fields.Datetime.context_timestamp( + self.user_demo, self.user_demo.write_date + ), + }, + ) + + def test_user_flat_model_serialization(self): + self.assertEqual( + UserFlatModel.from_orm(self.user_demo).dict(), + { + "id": self.user_demo.id, + "partner_id": self.user_demo.partner_id.id, + }, + ) + + +@skipIf(not PYDANTIC_V2, "Ignore because Pydantic < 2.0 is installed") +class TestGenericOdooGetterPydanticV2Case(CommonPydanticCase): + def test_user_model_serialization(self): + self.user_demo.partner_id.date = None + self.assertEqual( + UserModel.model_validate(self.user_demo, from_attributes=True).model_dump(), + { + "id": self.user_demo.id, + "partner": { + "id": self.user_demo.partner_id.id, + "name": self.user_demo.partner_id.name, + "date": None, + }, + }, + ) + + def test_user_model_serialization_date(self): + self.user_demo.partner_id.date = fields.Date.today() + self.assertEqual( + UserModel.model_validate(self.user_demo).partner.date, + self.user_demo.partner_id.date, + ) + + def test_user_model_details_serialization_datetime(self): + user_demo = self.user_demo.with_context(tz="Asia/Tokyo") + self.assertEqual( + UserDetailsModel.model_validate(user_demo).write_date, + fields.Datetime.context_timestamp(user_demo, user_demo.write_date), + ) + self.assertNotEqual( + UserDetailsModel.model_validate(user_demo).write_date.tzinfo, + fields.Datetime.context_timestamp( + self.user_demo, user_demo.write_date + ).tzinfo, + ) + + def test_user_details_model_serialization(self): + self.assertEqual( + UserDetailsModel.model_validate(self.user_demo).model_dump(), + { + "id": self.user_demo.id, + "partner": { + "id": self.user_demo.partner_id.id, + "name": self.user_demo.partner_id.name, + "date": None, + }, + "groups": [ + { + "id": group.id, + "name": group.name, + } + for group in self.user_demo.groups_id + ], + "action_id": None, + "signature": None, + "active": True, + "share": False, + "write_date": fields.Datetime.context_timestamp( + self.user_demo, self.user_demo.write_date + ), + }, + ) + + def test_user_flat_model_serialization(self): + self.assertEqual( + UserFlatModel.model_validate(self.user_demo).model_dump(), + { + "id": self.user_demo.id, + "partner_id": self.user_demo.partner_id.id, + }, + ) diff --git a/pydantic/utils.py b/pydantic/utils.py index 1be3434d3..fa229d0b4 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -3,66 +3,134 @@ from typing import Any -from odoo import fields, models - -from pydantic.utils import GetterDict - - -class GenericOdooGetter(GetterDict): - """A generic GetterDict for Odoo models - - The getter take care of casting one2many and many2many - field values to python list to allow the from_orm method from - pydantic class to work on odoo models. This getter is to specify - into the pydantic config. - - Usage: - - .. code-block:: python - - import pydantic - from odoo.addons.pydantic import models, utils +from packaging.version import Version - class Group(models.BaseModel): - name: str - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - class UserInfo(models.BaseModel): - name: str - groups: List[Group] = pydantic.Field(alias="groups_id") - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - user = self.env.user - user_info = UserInfo.from_orm(user) - - To avoid having to repeat the specific configuration required for the - `from_orm` method into each pydantic model, "odoo_orm_mode" can be used - as parent via the `_inherit` attribute +from odoo import fields, models - """ +from pydantic import __version__ as pydantic_version - def get(self, key: Any, default: Any = None) -> Any: - res = getattr(self._obj, key, default) - if isinstance(self._obj, models.BaseModel) and key in self._obj._fields: - field = self._obj._fields[key] - if res is False and field.type != "boolean": - return None - if field.type == "date" and not res: - return None - if field.type == "datetime": - if not res: +PYDANTIC_V2: bool = Version(pydantic_version) >= Version("2.0") + + +if PYDANTIC_V2: + from pydantic import ( + BaseModel, + ConfigDict, + ValidationInfo, + field_validator, + model_validator, + ) + + class PydanticOdooBaseModel(BaseModel): + """Pydantic BaseModel for odoo record + + This aims to help to serialize Odoo record + improving behavior like previous version: + + * Avoid False value on non boolean fields + * Convert Datetime to Datetime timezone aware + * if manyone + + """ + + model_config = ConfigDict( + from_attributes=True, + ) + + @field_validator("*", mode="before") + @classmethod + def odoo_validator_before(cls, value: Any, info: ValidationInfo): + odoo_record = info.config.get("odoo_record") + if odoo_record: + if info.field_name in odoo_record._fields: + field = odoo_record._fields[info.field_name] + if value is False and field.type != "boolean": + return None + if field.type == "datetime": + # Get the timestamp converted to the client's timezone. + # This call also add the tzinfo into the datetime object + return fields.Datetime.context_timestamp(odoo_record, value) + if field.type == "many2one": + if not value: + return None + if issubclass(cls.__annotations__.get(info.field_name), int): + # if field typing is an integer we return the .id + # (not the odoo record) + return value.id + return value + + @model_validator(mode="before") + @classmethod + def odoo_model_validator(cls, data: Any, info: ValidationInfo) -> Any: + info.config["odoo_record"] = ( + data if isinstance(data, models.BaseModel) else None + ) + return data + +else: + from pydantic.utils import GetterDict + + class GenericOdooGetter(GetterDict): + """A generic GetterDict for Odoo models + + The getter take care of casting one2many and many2many + field values to python list to allow the from_orm method from + pydantic class to work on odoo models. This getter is to specify + into the pydantic config. + + Usage: + + .. code-block:: python + + import pydantic + from odoo.addons.pydantic import models, utils + + class Group(models.BaseModel): + name: str + + class Config: + orm_mode = True + getter_dict = utils.GenericOdooGetter + + class UserInfo(models.BaseModel): + name: str + groups: List[Group] = pydantic.Field(alias="groups_id") + + class Config: + orm_mode = True + getter_dict = utils.GenericOdooGetter + + user = self.env.user + user_info = UserInfo.from_orm(user) + + To avoid having to repeat the specific configuration required for the + `from_orm` method into each pydantic model, "odoo_orm_mode" can be used + as parent via the `_inherit` attribute + + """ + + def get(self, key: Any, default: Any = None) -> Any: + res = getattr(self._obj, key, default) + if isinstance(self._obj, models.BaseModel) and key in self._obj._fields: + field = self._obj._fields[key] + if res is False and field.type != "boolean": + return None + if field.type == "date" and not res: + # PV: tests prove that this is useless, I'm wondering if + # there is case where data can be Falsy but not False? + # keeping to avoid regressions + return None + if field.type == "datetime": + if not res: + # I'm wondering if there is case where data can be + # Falsy but not False? + # keeping to avoid regressions + return None + # Get the timestamp converted to the client's timezone. + # This call also add the tzinfo into the datetime object + return fields.Datetime.context_timestamp(self._obj, res) + if field.type == "many2one" and not res: return None - # Get the timestamp converted to the client's timezone. - # This call also add the tzinfo into the datetime object - return fields.Datetime.context_timestamp(self._obj, res) - if field.type == "many2one" and not res: - return None - if field.type in ["one2many", "many2many"]: - return list(res) - return res + if field.type in ["one2many", "many2many"]: + return list(res) + return res From 3155cb27bc35295952a8a08df8b672bcbf264ada Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Tue, 29 Jul 2025 09:28:41 +0200 Subject: [PATCH 2/4] [CHG] pydantic: drop support pydantic v1 --- .github/workflows/test.yml | 14 +- pydantic/README.rst | 36 +--- pydantic/readme/DESCRIPTION.md | 2 +- pydantic/readme/USAGE.md | 29 +-- pydantic/static/description/index.html | 37 +--- .../test_pydantic_generic_odoo_getter.py | 105 ++-------- pydantic/utils.py | 182 +++++++----------- 7 files changed, 94 insertions(+), 311 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0e8c681ab..a06488079 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,11 +38,8 @@ jobs: - container: ghcr.io/oca/oca-ci/py3.10-odoo18.0:latest name: test with Odoo - container: ghcr.io/oca/oca-ci/py3.10-ocb18.0:latest - name: test with OCB and pydantic > 2.0 - pydantic2: "true" - - container: ghcr.io/oca/oca-ci/py3.10-ocb18.0:latest - name: test with OCB and pydantic < 2.0 - exclude: "fastapi" + name: test with OCB + makepot: "true" services: postgres: image: postgres:12.0 @@ -54,7 +51,6 @@ jobs: - 5432:5432 env: OCA_ENABLE_CHECKLOG_ODOO: "1" - EXCLUDE: "${{ matrix.exclude }}" steps: - uses: actions/checkout@v4 with: @@ -67,12 +63,6 @@ jobs: run: manifestoo -d . check-dev-status --default-dev-status=Beta - name: Initialize test db run: oca_init_test_database - - name: Ensure pydantic version < 2 - run: pip install "pydantic<2.0" - if: ${{ matrix.pydantic2 != 'true' }} - - name: Ensure pydantic version > 2 - run: pip install "pydantic>2.0" - if: ${{ matrix.pydantic2 == 'true' }} - name: Run tests run: oca_run_tests - uses: codecov/codecov-action@v4 diff --git a/pydantic/README.rst b/pydantic/README.rst index d8acaea95..4eb22b6a3 100644 --- a/pydantic/README.rst +++ b/pydantic/README.rst @@ -7,7 +7,7 @@ Pydantic !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:bf754ec770116cffb9eee07a08eb62409c1868dfd0f765c017156da1d8242df5 + !! source digest: sha256:0282f433b416c240729625a685a03a9775a264b4628001c237941a3fe506c13b !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -29,7 +29,7 @@ Pydantic |badge1| |badge2| |badge3| |badge4| |badge5| This addon provides a utility method that can be used to map odoo record -to a `Pydantic model `__. +to a `Pydantic model (>= v2) `__. If you need to make your Pydantic models extendable at runtime, takes a look at the python package @@ -49,10 +49,8 @@ To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping fields from odoo models to fields defined by the pydantic model. -To ease the mapping, the addon provide 2 utility classes: - -- Using ``pydantic>2.0``, - ``odoo.addons.pydantic.utils.PydanticOdooBaseModel``: +To ease the mapping, the addon provide an utility class (using +``pydantic>2.0``) ``odoo.addons.pydantic.utils.PydanticOdooBaseModel``: .. code:: python @@ -69,32 +67,6 @@ To ease the mapping, the addon provide 2 utility classes: user = self.env.user user_info = UserInfo.from_orm(user) -- Using ``pydantic<2.0``, - ``odoo.addons.pydantic.utils.GenericOdooGetter``: - -.. code:: python - - import pydantic - from odoo.addons.pydantic import utils - - class Group(pydantic.BaseModel): - name: str - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - class UserInfo(pydantic.BaseModel): - name: str - groups: List[Group] = pydantic.Field(alias="groups_id") - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - user = self.env.user - user_info = UserInfo.from_orm(user) - See the official `Pydantic documentation `__ to discover all the available functionalities. diff --git a/pydantic/readme/DESCRIPTION.md b/pydantic/readme/DESCRIPTION.md index a00840439..e05ef18f2 100644 --- a/pydantic/readme/DESCRIPTION.md +++ b/pydantic/readme/DESCRIPTION.md @@ -1,5 +1,5 @@ This addon provides a utility method that can be used to map odoo record -to a [Pydantic model](https://docs.pydantic.dev/). +to a [Pydantic model (>= v2)](https://docs.pydantic.dev/). If you need to make your Pydantic models extendable at runtime, takes a look at the python package diff --git a/pydantic/readme/USAGE.md b/pydantic/readme/USAGE.md index cfa4b6ce2..08e54f5aa 100644 --- a/pydantic/readme/USAGE.md +++ b/pydantic/readme/USAGE.md @@ -3,9 +3,7 @@ instances can be created from arbitrary odoo model instances by mapping fields from odoo models to fields defined by the pydantic model. -To ease the mapping, the addon provide 2 utility classes: - -* Using `pydantic>2.0`, `odoo.addons.pydantic.utils.PydanticOdooBaseModel`: +To ease the mapping, the addon provide an utility class (using `pydantic>2.0`) `odoo.addons.pydantic.utils.PydanticOdooBaseModel`: ``` python from odoo.addons.pydantic.utils import PydanticOdooBaseModel @@ -22,31 +20,6 @@ user = self.env.user user_info = UserInfo.from_orm(user) ``` -* Using `pydantic<2.0`, `odoo.addons.pydantic.utils.GenericOdooGetter`: - -``` python -import pydantic -from odoo.addons.pydantic import utils - -class Group(pydantic.BaseModel): - name: str - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - -class UserInfo(pydantic.BaseModel): - name: str - groups: List[Group] = pydantic.Field(alias="groups_id") - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - -user = self.env.user -user_info = UserInfo.from_orm(user) -``` - See the official [Pydantic documentation](https://docs.pydantic.dev/) to discover all the available functionalities. diff --git a/pydantic/static/description/index.html b/pydantic/static/description/index.html index 1b62e1f81..4639ee29a 100644 --- a/pydantic/static/description/index.html +++ b/pydantic/static/description/index.html @@ -371,7 +371,7 @@

Pydantic

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

This addon provides a utility method that can be used to map odoo record -to a Pydantic model.

+to a Pydantic model (>= v2).

If you need to make your Pydantic models extendable at runtime, takes a look at the python package extendable-pydantic @@ -396,11 +396,8 @@

Usage

To support pydantic models that map to Odoo models, Pydantic model instances can be created from arbitrary odoo model instances by mapping fields from odoo models to fields defined by the pydantic model.

-

To ease the mapping, the addon provide 2 utility classes:

-
    -
  • Using pydantic>2.0, -odoo.addons.pydantic.utils.PydanticOdooBaseModel:
  • -
+

To ease the mapping, the addon provide an utility class (using +pydantic>2.0) odoo.addons.pydantic.utils.PydanticOdooBaseModel:

 from odoo.addons.pydantic.utils import PydanticOdooBaseModel
 
@@ -412,32 +409,6 @@ 

Usage

name: str groups: List[Group] = pydantic.Field(alias="groups_id") -user = self.env.user -user_info = UserInfo.from_orm(user) -
-
    -
  • Using pydantic<2.0, -odoo.addons.pydantic.utils.GenericOdooGetter:
  • -
-
-import pydantic
-from odoo.addons.pydantic import utils
-
-class Group(pydantic.BaseModel):
-    name: str
-
-    class Config:
-        orm_mode = True
-        getter_dict = utils.GenericOdooGetter
-
-class UserInfo(pydantic.BaseModel):
-    name: str
-    groups: List[Group] = pydantic.Field(alias="groups_id")
-
-    class Config:
-        orm_mode = True
-        getter_dict = utils.GenericOdooGetter
-
 user = self.env.user
 user_info = UserInfo.from_orm(user)
 
@@ -493,4 +464,4 @@

Maintainers

- + \ No newline at end of file diff --git a/pydantic/tests/test_pydantic_generic_odoo_getter.py b/pydantic/tests/test_pydantic_generic_odoo_getter.py index 0ca049486..8f1030c1f 100644 --- a/pydantic/tests/test_pydantic_generic_odoo_getter.py +++ b/pydantic/tests/test_pydantic_generic_odoo_getter.py @@ -1,23 +1,11 @@ import datetime -from unittest import skipIf from odoo import fields from odoo.tests import TransactionCase -from pydantic import BaseModel, Field +from pydantic import Field -from ..utils import PYDANTIC_V2 - -if PYDANTIC_V2: - from ..utils import PydanticOdooBaseModel as PydanticOrmBaseModel - -else: - from ..utils import GenericOdooGetter - - class PydanticOrmBaseModel(BaseModel): - class Config: - orm_mode = True - getter_dict = GenericOdooGetter +from ..utils import PydanticOdooBaseModel as PydanticOrmBaseModel class OdooBaseModel(PydanticOrmBaseModel): @@ -60,80 +48,6 @@ def setUpClass(cls): cls.user_demo.share = False -@skipIf(PYDANTIC_V2, "Ignore because Pydantic >= 2.0 is installed") -class TestGenericOdooGetterPydanticV1Case(CommonPydanticCase): - def test_user_model_serialization(self): - self.user_demo.partner_id.date = None - self.assertEqual( - UserModel.from_orm(self.user_demo).dict(), - { - "id": self.user_demo.id, - "partner": { - "id": self.user_demo.partner_id.id, - "name": self.user_demo.partner_id.name, - "date": None, - }, - }, - ) - - def test_user_model_serialization_date(self): - self.user_demo.partner_id.date = fields.Date.today() - self.assertEqual( - UserModel.from_orm(self.user_demo).partner.date, - self.user_demo.partner_id.date, - ) - - def test_user_model_details_serialization_datetime(self): - user_demo = self.user_demo.with_context(tz="Asia/Tokyo") - self.assertEqual( - UserDetailsModel.from_orm(user_demo).write_date, - fields.Datetime.context_timestamp(user_demo, user_demo.write_date), - ) - self.assertNotEqual( - UserDetailsModel.from_orm(user_demo).write_date.tzinfo, - fields.Datetime.context_timestamp( - self.user_demo, user_demo.write_date - ).tzinfo, - ) - - def test_user_details_model_serialization(self): - self.assertEqual( - UserDetailsModel.from_orm(self.user_demo).dict(), - { - "id": self.user_demo.id, - "partner": { - "id": self.user_demo.partner_id.id, - "name": self.user_demo.partner_id.name, - "date": None, - }, - "groups": [ - { - "id": group.id, - "name": group.name, - } - for group in self.user_demo.groups_id - ], - "action_id": None, - "signature": None, - "active": True, - "share": False, - "write_date": fields.Datetime.context_timestamp( - self.user_demo, self.user_demo.write_date - ), - }, - ) - - def test_user_flat_model_serialization(self): - self.assertEqual( - UserFlatModel.from_orm(self.user_demo).dict(), - { - "id": self.user_demo.id, - "partner_id": self.user_demo.partner_id.id, - }, - ) - - -@skipIf(not PYDANTIC_V2, "Ignore because Pydantic < 2.0 is installed") class TestGenericOdooGetterPydanticV2Case(CommonPydanticCase): def test_user_model_serialization(self): self.user_demo.partner_id.date = None @@ -204,3 +118,18 @@ def test_user_flat_model_serialization(self): "partner_id": self.user_demo.partner_id.id, }, ) + + def test_not_an_odoo_record(self): + user = UserDetailsModel( + id=666, + partner_id={"id": 66, "name": "test"}, + groups_id=[{"id": 33, "name": "group 1"}], + action_id={"id": 55}, + signature=None, + active=True, + share=False, + write_date=fields.Datetime.now(), + ) + self.assertEqual( + UserDetailsModel.model_validate(user).model_dump(), user.model_dump() + ) diff --git a/pydantic/utils.py b/pydantic/utils.py index fa229d0b4..f1489e386 100644 --- a/pydantic/utils.py +++ b/pydantic/utils.py @@ -3,134 +3,82 @@ from typing import Any -from packaging.version import Version - from odoo import fields, models -from pydantic import __version__ as pydantic_version - -PYDANTIC_V2: bool = Version(pydantic_version) >= Version("2.0") +from pydantic import ( + BaseModel, + ConfigDict, + ValidationInfo, + field_validator, + model_validator, +) -if PYDANTIC_V2: - from pydantic import ( - BaseModel, - ConfigDict, - ValidationInfo, - field_validator, - model_validator, - ) - - class PydanticOdooBaseModel(BaseModel): - """Pydantic BaseModel for odoo record +class PydanticOdooBaseModel(BaseModel): + """Pydantic BaseModel for odoo record - This aims to help to serialize Odoo record - improving behavior like previous version: + This aims to help to serialize Odoo record + improving behavior like previous version: - * Avoid False value on non boolean fields - * Convert Datetime to Datetime timezone aware - * if manyone + * Avoid False value on non boolean fields + * Convert Datetime to Datetime timezone aware + * using int type on many2one return the foreign key id + (not the odoo record) + """ - """ + model_config = ConfigDict( + from_attributes=True, + ) - model_config = ConfigDict( - from_attributes=True, + @classmethod + def model_validate( + cls, + obj: Any, + *, + context: Any | None = None, + **kwargs, + ): + if context is None: + context = {} + + if "odoo_records" not in context: + context["odoo_records"] = {} + + return super().model_validate( + obj, + context=context, + **kwargs, ) - @field_validator("*", mode="before") - @classmethod - def odoo_validator_before(cls, value: Any, info: ValidationInfo): - odoo_record = info.config.get("odoo_record") - if odoo_record: - if info.field_name in odoo_record._fields: - field = odoo_record._fields[info.field_name] - if value is False and field.type != "boolean": - return None - if field.type == "datetime": - # Get the timestamp converted to the client's timezone. - # This call also add the tzinfo into the datetime object - return fields.Datetime.context_timestamp(odoo_record, value) - if field.type == "many2one": - if not value: - return None - if issubclass(cls.__annotations__.get(info.field_name), int): - # if field typing is an integer we return the .id - # (not the odoo record) - return value.id - return value - - @model_validator(mode="before") - @classmethod - def odoo_model_validator(cls, data: Any, info: ValidationInfo) -> Any: - info.config["odoo_record"] = ( - data if isinstance(data, models.BaseModel) else None - ) - return data - -else: - from pydantic.utils import GetterDict - - class GenericOdooGetter(GetterDict): - """A generic GetterDict for Odoo models - - The getter take care of casting one2many and many2many - field values to python list to allow the from_orm method from - pydantic class to work on odoo models. This getter is to specify - into the pydantic config. - - Usage: - - .. code-block:: python - - import pydantic - from odoo.addons.pydantic import models, utils - - class Group(models.BaseModel): - name: str - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - class UserInfo(models.BaseModel): - name: str - groups: List[Group] = pydantic.Field(alias="groups_id") - - class Config: - orm_mode = True - getter_dict = utils.GenericOdooGetter - - user = self.env.user - user_info = UserInfo.from_orm(user) - - To avoid having to repeat the specific configuration required for the - `from_orm` method into each pydantic model, "odoo_orm_mode" can be used - as parent via the `_inherit` attribute - - """ - - def get(self, key: Any, default: Any = None) -> Any: - res = getattr(self._obj, key, default) - if isinstance(self._obj, models.BaseModel) and key in self._obj._fields: - field = self._obj._fields[key] - if res is False and field.type != "boolean": - return None - if field.type == "date" and not res: - # PV: tests prove that this is useless, I'm wondering if - # there is case where data can be Falsy but not False? - # keeping to avoid regressions + @field_validator("*", mode="before") + @classmethod + def odoo_validator_before(cls, value: Any, info: ValidationInfo): + odoo_record = info.context and info.context.get("odoo_records").get( + info.config.get("title") + ) + if odoo_record is not None: + if info.field_name in odoo_record._fields: + field = odoo_record._fields[info.field_name] + if value is False and field.type != "boolean": return None if field.type == "datetime": - if not res: - # I'm wondering if there is case where data can be - # Falsy but not False? - # keeping to avoid regressions - return None # Get the timestamp converted to the client's timezone. # This call also add the tzinfo into the datetime object - return fields.Datetime.context_timestamp(self._obj, res) - if field.type == "many2one" and not res: - return None - if field.type in ["one2many", "many2many"]: - return list(res) - return res + return fields.Datetime.context_timestamp(odoo_record, value) + if field.type == "many2one": + if not value: + return None + if issubclass(cls.__annotations__.get(info.field_name), int): + # if field typing is an integer we return the .id + # (not the odoo record) + return value.id + return value + + @model_validator(mode="before") + @classmethod + def odoo_model_validator(cls, data: Any, info: ValidationInfo) -> Any: + if isinstance(info.context, dict): + info.context["odoo_records"][info.config.get("title")] = ( + data if isinstance(data, models.BaseModel) else None + ) + return data From 345129ec2d09af230b6527b97a0746a3b6a72ae8 Mon Sep 17 00:00:00 2001 From: Thomas Binsfeld Date: Wed, 17 Sep 2025 15:04:23 +0200 Subject: [PATCH 3/4] [MIG] pydantic v2: use res.currency.rate to test the date serialization date field on res.partner has been removed in v18 --- .../test_pydantic_generic_odoo_getter.py | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/pydantic/tests/test_pydantic_generic_odoo_getter.py b/pydantic/tests/test_pydantic_generic_odoo_getter.py index 8f1030c1f..28cc2c6d6 100644 --- a/pydantic/tests/test_pydantic_generic_odoo_getter.py +++ b/pydantic/tests/test_pydantic_generic_odoo_getter.py @@ -14,7 +14,6 @@ class OdooBaseModel(PydanticOrmBaseModel): class PartnerModel(OdooBaseModel): name: str - date: datetime.date | None = None class UserFlatModel(OdooBaseModel): @@ -38,6 +37,11 @@ class UserDetailsModel(UserModel): write_date: datetime.datetime +class CurrencyRateModel(OdooBaseModel): + name: datetime.date | None = None + rate: float + + class CommonPydanticCase(TransactionCase): @classmethod def setUpClass(cls): @@ -46,28 +50,29 @@ def setUpClass(cls): cls.user_demo.action_id = False cls.user_demo.signature = False cls.user_demo.share = False + cls.currency_eur = cls.env.ref("base.USD") + cls.currency_rate = cls.env.ref("base.rateUSD") class TestGenericOdooGetterPydanticV2Case(CommonPydanticCase): def test_user_model_serialization(self): - self.user_demo.partner_id.date = None + self.currency_rate.name = None # name is a date field self.assertEqual( - UserModel.model_validate(self.user_demo, from_attributes=True).model_dump(), + CurrencyRateModel.model_validate( + self.currency_rate, from_attributes=True + ).model_dump(), { - "id": self.user_demo.id, - "partner": { - "id": self.user_demo.partner_id.id, - "name": self.user_demo.partner_id.name, - "date": None, - }, + "id": self.currency_rate.id, + "name": None, + "rate": self.currency_rate.rate, }, ) def test_user_model_serialization_date(self): - self.user_demo.partner_id.date = fields.Date.today() + self.currency_rate.name = fields.Date.today() # name is a date field self.assertEqual( - UserModel.model_validate(self.user_demo).partner.date, - self.user_demo.partner_id.date, + CurrencyRateModel.model_validate(self.currency_rate).name, + self.currency_rate.name, ) def test_user_model_details_serialization_datetime(self): @@ -91,7 +96,6 @@ def test_user_details_model_serialization(self): "partner": { "id": self.user_demo.partner_id.id, "name": self.user_demo.partner_id.name, - "date": None, }, "groups": [ { From 72539fc12c0828afbe425b45656053878ba29b90 Mon Sep 17 00:00:00 2001 From: Thomas Binsfeld Date: Wed, 17 Sep 2025 15:16:56 +0200 Subject: [PATCH 4/4] [REF] fastapi: fix depreciation warning DeprecationWarning: 'HTTP_422_UNPROCESSABLE_ENTITY' is deprecated. Use 'HTTP_422_UNPROCESSABLE_CONTENT' instead. --- fastapi/error_handlers.py | 2 +- fastapi/tests/test_fastapi.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fastapi/error_handlers.py b/fastapi/error_handlers.py index 2e4202d6b..192ef4064 100644 --- a/fastapi/error_handlers.py +++ b/fastapi/error_handlers.py @@ -29,7 +29,7 @@ def convert_exception_to_status_body(exc: Exception) -> tuple[int, dict]: status_code = exc.status_code details = exc.detail elif isinstance(exc, RequestValidationError): - status_code = status.HTTP_422_UNPROCESSABLE_ENTITY + status_code = status.HTTP_422_UNPROCESSABLE_CONTENT details = jsonable_encoder(exc.errors()) elif isinstance(exc, WebSocketRequestValidationError): status_code = status.WS_1008_POLICY_VIOLATION diff --git a/fastapi/tests/test_fastapi.py b/fastapi/tests/test_fastapi.py index fbda932fb..c9eae09b8 100644 --- a/fastapi/tests/test_fastapi.py +++ b/fastapi/tests/test_fastapi.py @@ -156,7 +156,9 @@ def test_request_validation_error(self) -> None: route = "/fastapi_demo/demo/exception?exception_type=BAD&error_message=" response = self.url_open(route, timeout=200) mocked_commit.assert_not_called() - self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) + self.assertEqual( + response.status_code, status.HTTP_422_UNPROCESSABLE_CONTENT + ) def test_no_commit_on_exception(self) -> None: # this test check that the way we mock the cursor is working as expected