Skip to content

Commit 8787eaa

Browse files
qgroulardlmignon
authored andcommitted
[IMP] Odoo getter: Better handle empty fields
1 parent e7ab678 commit 8787eaa

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pydantic/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import Any
55

6-
from odoo import models
6+
from odoo import fields, models
77

88
from pydantic.utils import GetterDict
99

@@ -51,6 +51,18 @@ def get(self, key: Any, default: Any = None) -> Any:
5151
res = getattr(self._obj, key, default)
5252
if isinstance(self._obj, models.BaseModel) and key in self._obj._fields:
5353
field = self._obj._fields[key]
54+
if res is False and field.type != "boolean":
55+
return None
56+
if field.type == "date" and not res:
57+
return None
58+
if field.type == "datetime":
59+
if not res:
60+
return None
61+
# Get the timestamp converted to the client's timezone.
62+
# This call also add the tzinfo into the datetime object
63+
return fields.Datetime.context_timestamp(self._obj, res)
64+
if field.type == "many2one" and not res:
65+
return None
5466
if field.type in ["one2many", "many2many"]:
5567
return list(res)
5668
return res

0 commit comments

Comments
 (0)