diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 367dea5f5..5dd29032f 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -5,7 +5,6 @@ exclude: |
^base_rest_auth_api_key/|
^base_rest_pydantic/|
^extendable/|
- ^pydantic/|
^rest_log/|
# END NOT INSTALLABLE ADDONS
# Files and folders generated by bots, to avoid loops
diff --git a/pydantic/README.rst b/pydantic/README.rst
index cddfbb432..cdcc7a23b 100644
--- a/pydantic/README.rst
+++ b/pydantic/README.rst
@@ -11,7 +11,7 @@ Pydantic
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !! source digest: sha256:bf754ec770116cffb9eee07a08eb62409c1868dfd0f765c017156da1d8242df5
+ !! source digest: sha256:cffcb5f5c45000bae7dfe3cbf3c9c8c39e5573567ec1b08c2ee4bf56e4f24a18
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -21,25 +21,25 @@ Pydantic
: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
- :target: https://github.com/OCA/rest-framework/tree/18.0/pydantic
+ :target: https://github.com/OCA/rest-framework/tree/19.0/pydantic
:alt: OCA/rest-framework
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
- :target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-pydantic
+ :target: https://translation.odoo-community.org/projects/rest-framework-19-0/rest-framework-19-0-pydantic
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
- :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=18.0
+ :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=19.0
:alt: Try me on Runboat
|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
`extendable-pydantic `__
-and the odoo addon
-`extendable `__
+and the `odoo addon
+extendable `__
**Table of contents**
@@ -51,36 +51,28 @@ 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 an utility class (using
+``pydantic>2.0``) ``odoo.addons.pydantic.utils.PydanticOdooBaseModel``:
.. code:: python
- import pydantic
- from odoo.addons.pydantic import utils
+ from odoo.addons.pydantic.utils import PydanticOdooBaseModel
- class Group(pydantic.BaseModel):
- name: str
- class Config:
- orm_mode = True
- getter_dict = utils.GenericOdooGetter
+ class Group(PydanticOdooBaseModel):
+ name: str
- class UserInfo(pydantic.BaseModel):
+ class UserInfo(PydanticOdooBaseModel):
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.
+See the official `Pydantic documentation `__
+to discover all the available functionalities.
Known issues / Roadmap
======================
@@ -97,7 +89,7 @@ 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
-`feedback `_.
+`feedback `_.
Do not contact contributors directly about support or help with technical issues.
@@ -114,6 +106,7 @@ Contributors
- Laurent Mignon
- Tris Doan
+- Pierre Verkest
Maintainers
-----------
@@ -136,6 +129,6 @@ Current `maintainer `__:
|maintainer-lmignon|
-This module is part of the `OCA/rest-framework `_ project on GitHub.
+This module is part of the `OCA/rest-framework `_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
diff --git a/pydantic/__manifest__.py b/pydantic/__manifest__.py
index 5e72a0079..4152b26da 100644
--- a/pydantic/__manifest__.py
+++ b/pydantic/__manifest__.py
@@ -5,7 +5,7 @@
"name": "Pydantic",
"summary": """
Utility addon to ease mapping between Pydantic and Odoo models""",
- "version": "18.0.1.0.1",
+ "version": "19.0.1.0.0",
"development_status": "Beta",
"license": "LGPL-3",
"maintainers": ["lmignon"],
@@ -17,5 +17,5 @@
"external_dependencies": {
"python": ["pydantic>=2.0.0", "contextvars", "typing-extensions"]
},
- "installable": False,
+ "installable": True,
}
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..e05ef18f2 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 (>= v2)](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..08e54f5aa 100644
--- a/pydantic/readme/USAGE.md
+++ b/pydantic/readme/USAGE.md
@@ -1,32 +1,25 @@
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 an utility class (using `pydantic>2.0`) `odoo.addons.pydantic.utils.PydanticOdooBaseModel`:
``` python
-import pydantic
-from odoo.addons.pydantic import utils
+from odoo.addons.pydantic.utils import PydanticOdooBaseModel
-class Group(pydantic.BaseModel):
- name: str
- class Config:
- orm_mode = True
- getter_dict = utils.GenericOdooGetter
+class Group(PydanticOdooBaseModel):
+ name: str
-class UserInfo(pydantic.BaseModel):
+class UserInfo(PydanticOdooBaseModel):
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://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..cff00cc0a 100644
--- a/pydantic/static/description/index.html
+++ b/pydantic/static/description/index.html
@@ -372,16 +372,16 @@ Pydantic
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-!! source digest: sha256:bf754ec770116cffb9eee07a08eb62409c1868dfd0f765c017156da1d8242df5
+!! source digest: sha256:cffcb5f5c45000bae7dfe3cbf3c9c8c39e5573567ec1b08c2ee4bf56e4f24a18
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
-

+

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
-and the odoo addon
-extendable
+and the odoo addon
+extendable
Table of contents
@@ -400,34 +400,25 @@ Pydantic
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 an utility class (using
+pydantic>2.0) odoo.addons.pydantic.utils.PydanticOdooBaseModel:
-import pydantic
-from odoo.addons.pydantic import utils
+from odoo.addons.pydantic.utils import PydanticOdooBaseModel
-class Group(pydantic.BaseModel):
- name: str
- class Config:
- orm_mode = True
- getter_dict = utils.GenericOdooGetter
+class Group(PydanticOdooBaseModel):
+ name: str
-class UserInfo(pydantic.BaseModel):
+class UserInfo(PydanticOdooBaseModel):
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.
+
See the official Pydantic documentation
+to discover all the available functionalities.
@@ -442,7 +433,7 @@
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
-feedback.
+
feedback.
Do not contact contributors directly about support or help with technical issues.
@@ -471,7 +463,7 @@
promote its widespread use.
Current maintainer:

-
This module is part of the OCA/rest-framework project on GitHub.
+
This module is part of the OCA/rest-framework project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
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..6919e5a1f
--- /dev/null
+++ b/pydantic/tests/test_pydantic_generic_odoo_getter.py
@@ -0,0 +1,139 @@
+import datetime
+
+from odoo import fields
+from odoo.tests import TransactionCase
+
+from pydantic import Field
+
+from ..utils import PydanticOdooBaseModel as PydanticOrmBaseModel
+
+
+class OdooBaseModel(PydanticOrmBaseModel):
+ id: int
+
+
+class PartnerModel(OdooBaseModel):
+ name: str
+
+
+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="group_ids")
+ action_id: OdooBaseModel | None = None
+ signature: str | None = None
+ active: bool | None = None
+ share: bool | None = None
+ write_date: datetime.datetime
+
+
+class CurrencyRateModel(OdooBaseModel):
+ name: datetime.date | None = None
+ rate: float
+
+
+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
+ 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.currency_rate.name = None # name is a date field
+ self.assertEqual(
+ CurrencyRateModel.model_validate(
+ self.currency_rate, from_attributes=True
+ ).model_dump(),
+ {
+ "id": self.currency_rate.id,
+ "name": None,
+ "rate": self.currency_rate.rate,
+ },
+ )
+
+ def test_user_model_serialization_date(self):
+ self.currency_rate.name = fields.Date.today() # name is a date field
+ self.assertEqual(
+ CurrencyRateModel.model_validate(self.currency_rate).name,
+ self.currency_rate.name,
+ )
+
+ 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,
+ },
+ "groups": [
+ {
+ "id": group.id,
+ "name": group.name,
+ }
+ for group in self.user_demo.group_ids
+ ],
+ "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,
+ },
+ )
+
+ 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 1be3434d3..f1489e386 100644
--- a/pydantic/utils.py
+++ b/pydantic/utils.py
@@ -5,64 +5,80 @@
from odoo import fields, models
-from pydantic.utils import GetterDict
+from pydantic import (
+ BaseModel,
+ ConfigDict,
+ ValidationInfo,
+ field_validator,
+ model_validator,
+)
-class GenericOdooGetter(GetterDict):
- """A generic GetterDict for Odoo models
+class PydanticOdooBaseModel(BaseModel):
+ """Pydantic BaseModel for odoo record
- 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
+ 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
+ * using int type on many2one return the foreign key id
+ (not the odoo record)
"""
- 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:
+ 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.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
- # 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 == "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:
+ if isinstance(info.context, dict):
+ info.context["odoo_records"][info.config.get("title")] = (
+ data if isinstance(data, models.BaseModel) else None
+ )
+ return data
diff --git a/requirements.txt b/requirements.txt
index bcba9b734..8e0407129 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,9 @@
# generated from manifests external_dependencies
a2wsgi>=1.10.6
+contextvars
fastapi>=0.110.0
parse-accept-language
+pydantic>=2.0.0
python-multipart
+typing-extensions
ujson