Search 1.9 billion lines of Odoo code on GitHub

datamodel

Author: ACSONE SA/NV, Odoo Community Association (OCA)
License: LGPL-3
Branch: 14.0-abi_master
Repository: acsone/rest-framework
Languages: HTML (412, 35.6%), Python (682, 58.9%), and reStructuredText (63, 5.4%)
Other branches: 12.0, 12.0-base_rest-cors-options, 12.0-datamodel-experiment, 12.0-new-api-wip, 13.0, 13.0-48-port, 13.0-backport-base-rest-pydantic, 13.0-backport-extendable, 13.0-base_rest-cors-options, 13.0-fixcrashdesc, 14.0, 14.0-add-pydantic, 14.0-base-rest-pydantic-lmi, 14.0-base_res_service_add_properties-qgr, 14.0-deprecated-implicit-rest-method, 14.0-fdl_master, 14.0-fix-path-parameter-name, 14.0-fix-response-processing, 14.0-fixcrashdesc, 14.0-moz_master, 15.0, 15.0-initial, 16.0, 16.0-fastapi, 16.0-upgrade-pre-commit, and oca-port-pr--from-14.0-to-13.0
Other repositories: Change2improve/rest-framework, Digital5-Odoo/rest-framework, Gabinete-Digital/rest-framework, NeatNerdPrime/rest-framework, OCA/rest-framework, SeuMarco/rest-framework, akretion/rest-framework, coopiteasy/rest-framework, gastonfeng/rest-framework, guewen/rest-framework, gurneyalex/rest-framework, odoogap/rest-framework, simahawk/rest-framework, ursais/rest-framework, and wahello/rest-framework

<h1 class="title">Datamodel</h1> <p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/rest-framework/tree/14.0/datamodel"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/rest-framework-14-0/rest-framework-14-0-datamodel"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/271/14.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p> <p>This addon allows you to define simple data models supporting serialization/deserialization to/from json</p> <p>Datamodels are <a class="reference external" href="https://github.com/sv-tools/marshmallow-objects">Marshmallow models</a> classes that can be inherited as Odoo Models.</p> <p><strong>Table of contents</strong></p> <div class="contents local topic" id="contents"> <ul class="simple"> <li><a class="reference internal" href="#usage" id="id1">Usage</a></li> <li><a class="reference internal" href="#known-issues-roadmap" id="id2">Known issues / Roadmap</a></li> <li><a class="reference internal" href="#bug-tracker" id="id3">Bug Tracker</a></li> <li><a class="reference internal" href="#credits" id="id4">Credits</a><ul> <li><a class="reference internal" href="#authors" id="id5">Authors</a></li> <li><a class="reference internal" href="#contributors" id="id6">Contributors</a></li> <li><a class="reference internal" href="#maintainers" id="id7">Maintainers</a></li> </ul> </li> </ul> </div> <a name="usage"></a> <h2><a class="toc-backref" href="#id1">Usage</a></h2> <p>To define your own datamodel you just need to create a class that inherits from <code>odoo.addons.datamodel.core.Datamodel</code></p> <pre> <code lang="python">from marshmallow import fields from odoo.addons.base_rest import restapi from odoo.addons.component.core import Component from odoo.addons.datamodel.core import Datamodel class PartnerShortInfo(Datamodel): _name = &quot;partner.short.info&quot; id = fields.Integer(required=True, allow_none=False) name = fields.String(required=True, allow_none=False) class PartnerInfo(Datamodel): _name = &quot;partner.info&quot; _inherit = &quot;partner.short.info&quot; street = fields.String(required=True, allow_none=False) street2 = fields.String(required=False, allow_none=True) zip_code = fields.String(required=True, allow_none=False) city = fields.String(required=True, allow_none=False) phone = fields.String(required=False, allow_none=True) is_componay = fields.Boolean(required=False, allow_none=False)</code> </pre> <p>As for odoo models, you can extend the <cite>base</cite> datamodel by inheriting of <cite>base</cite>.</p> <pre> <code lang="python">class Base(Datamodel): _inherit = &quot;base&quot; def _my_method(self): pass</code> </pre> <p>Datamodels are available through the <cite>datamodels</cite> registry provided by the Odoo's environment.</p> <pre> <code lang="python">class ResPartner(Model): _inherit = &quot;res.partner&quot; def _to_partner_info(self): PartnerInfo = self.env.datamodels[&quot;partner.info&quot;] partner_info = PartnerInfo(partial=True) partner_info.id = partner.id partner_info.name = partner.name partner_info.street = partner.street partner_info.street2 = partner.street2 partner_info.zip_code = partner.zip partner_info.city = partner.city partner_info.phone = partner.phone partner_info.is_company = partner.is_company return partner_info</code> </pre> <p>The Odoo's environment is also available into the datamodel instance.</p> <pre> <code lang="python">class MyDataModel(Datamodel): _name = &quot;my.data.model&quot; def _my_method(self): partners = self.env[&quot;res.partner&quot;].search([])</code> </pre> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">The <cite>env</cite> property into a Datamodel instance is mutable. IOW, you can't rely on information (context, user) provided by the environment. The <cite>env</cite> property is a helper property that give you access to the odoo's registry and must be use with caution.</p> </div> <a name="known-issues-roadmap"></a> <h2><a class="toc-backref" href="#id2">Known issues / Roadmap</a></h2> <p>The <a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement+label%3Adatamodel">roadmap</a> and <a class="reference external" href="https://github.com/OCA/rest-framework/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Adatamodel">known issues</a> can be found on GitHub.</p> <a name="bug-tracker"></a> <h2><a class="toc-backref" href="#id3">Bug Tracker</a></h2> <p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed <a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20datamodel%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p> <p>Do not contact contributors directly about support or help with technical issues.</p> <a name="credits"></a> <h2><a class="toc-backref" href="#id4">Credits</a></h2> <a name="authors"></a> <h3><a class="toc-backref" href="#id5">Authors</a></h3> <ul class="simple"> <li>ACSONE SA/NV</li> </ul> <a name="contributors"></a> <h3><a class="toc-backref" href="#id6">Contributors</a></h3> <ul> <li><p class="first">Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</p> </li> <li><p class="first"><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:</p> <blockquote> <ul class="simple"> <li>Carlos Roca</li> </ul> </blockquote> </li> </ul> <a name="maintainers"></a> <h3><a class="toc-backref" href="#id7">Maintainers</a></h3> <p>This module is maintained by the OCA.</p> <a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a> <p>OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.</p> <p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p> <p><a class="reference external" href="https://github.com/lmignon"><img alt="lmignon" src="https://github.com/lmignon.png?size=40px" /></a></p> <p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/14.0/datamodel">OCA/rest-framework</a> project on GitHub.</p> <p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>