Author: |
Wakari, Odoo Community Association (OCA) |
License: |
LGPL-3 |
Branch: |
16.0 |
Repository: |
acsone/rest-framework |
Dependencies: |
datamodel |
Languages: |
HTML (445, 37.5%),
Python (564, 47.6%),
and
reStructuredText (177, 14.9%) |
Other branches: |
13.0-backport-base-rest-pydantic,
13.0-backport-extendable,
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-response-processing,
14.0-moz_master,
15.0,
15.0-initial,
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,
OCA/rest-framework,
SeuMarco/rest-framework,
akretion/rest-framework,
coopiteasy/rest-framework,
gastonfeng/rest-framework,
gurneyalex/rest-framework,
odoogap/rest-framework,
simahawk/rest-framework,
and
ursais/rest-framework |
<h1 class="title">Model Serializer</h1>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.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/15.0/model_serializer"><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-15-0/rest-framework-15-0-model_serializer"><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/15.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module takes advantage of the concepts introduced in the <cite>datamodel</cite> module to offer mechanisms similar to
(a subset of) the <cite>ModelSerializer</cite> in Django REST Framework. That is, use the definition of the Odoo model to
partially automate the definition of a corresponding <cite>Datamodel</cite> class.</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
<a class="reference external" href="https://odoo-community.org/page/development-status">More details on development status</a></p>
</div>
<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="id3">Usage</a><ul>
<li><a class="reference internal" href="#basic-usage" id="id4">Basic usage</a></li>
<li><a class="reference internal" href="#overriding-fields-definition" id="id5">Overriding fields definition</a></li>
<li><a class="reference internal" href="#de-serialization" id="id6">(De)serialization</a></li>
</ul>
</li>
<li><a class="reference internal" href="#changelog" id="id7">Changelog</a><ul>
<li><a class="reference internal" href="#id1" id="id8">14.0.1.0.0</a></li>
<li><a class="reference internal" href="#id2" id="id9">15.0.1.0.0</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bug-tracker" id="id10">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id11">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id12">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id13">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id14">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<a name="usage"></a>
<h2 class="with-subtitle"><a class="toc-backref" href="#id3">Usage</a></h2>
<h2 class="section-subtitle" id="modelserializer-class"><span class="section-subtitle"><code class="code">ModelSerializer</code> class</span></h2>
<p>The <code class="code">ModelSerializer</code> class inherits from the <code class="code">Datamodel</code> class and adds functionalities. Therefore any
class inheriting from <code class="code">ModelSerializer</code> can be used the exact same way as any other <code class="code">Datamodel</code>.</p>
<a name="basic-usage"></a>
<h3><a class="toc-backref" href="#id4">Basic usage</a></h3>
<p>Here is a basic example:</p>
<pre>
<code>from odoo.addons.model_serializer.core import ModelSerializer
class PartnerInfo(ModelSerializer):
_name = "partner.info"
_model = "res.partner"
_model_fields = ["id", "name", "country_id"]</code>
</pre>
<p>The result is equivalent to the following <code class="code">Datamodel</code> classes:</p>
<pre>
<code>from marshmallow import fields
from odoo.addons.datamodel.core import Datamodel
from odoo.addons.datamodel.fields import NestedModel
class PartnerInfo(Datamodel):
_name = "partner.info"
id = fields.Integer(required=True, allow_none=False, dump_only=True)
name = fields.String(required=True, allow_none=False)
country = NestedModel("_auto_nested_serializer.res.country")
class _AutoNestedSerializerResCountry(Datamodel):
_name = "_auto_nested_serializer.res.country"
id = fields.Integer(required=True, allow_none=False, dump_only=True)
display_name = fields.String(dump_only=True)</code>
</pre>
<a name="overriding-fields-definition"></a>
<h3><a class="toc-backref" href="#id5">Overriding fields definition</a></h3>
<p>It is possible to override the default definition of fields as such:</p>
<pre>
<code>from odoo.addons.model_serializer.core import ModelSerializer
class PartnerInfo(ModelSerializer):
_name = "partner.info"
_model = "res.partner"
_model_fields = ["id", "name", "country_id"]
country_id = NestedModel("country.info")
class CountryInfo(ModelSerializer):
_name = "country.info"
_model = "res.country"
_model_fields = ["code", "name"]</code>
</pre>
<p>In this example, we override a <code class="code">NestedModel</code> but it works the same for any other field type.</p>
<a name="de-serialization"></a>
<h3><a class="toc-backref" href="#id6">(De)serialization</a></h3>
<p><code class="code">ModelSerializer</code> does all the heavy-lifting of transforming a <code class="code">Datamodel</code> instance into the corresponding
<code class="code">recordset</code>, and vice-versa.</p>
<p>To transform a recordset into a (list of) <code class="code">ModelSerializer</code> instance(s) (serialization), do the following:</p>
<pre>
<code>partner_info = self.env.datamodels["partner.info"].from_recordset(partner)</code>
</pre>
<p>This will return a single instance; if your recordset contains more than one record, you can get a list of instances
by passing <code class="code">many=True</code> to this method.</p>
<p>To transform a <code class="code">ModelSerializer</code> instance into a recordset (de-serialization), do the following:</p>
<pre>
<code>partner = partner_info.to_recordset()</code>
</pre>
<p>Unless an existing partner can be found (see below), this method <strong>creates a new record</strong> in the database. You can avoid
that by passing <code class="code">create=False</code>, in which case the system will only create them in memory (<code class="code">NewId</code> recordset).</p>
<p>In order to determine if the corresponding Odoo record already exists or if a new one should be created, the system
checks by default if the <code class="code">id</code> field of the instance corresponds to a database record. This default behavior can be
modified like so:</p>
<pre>
<code>class CountryInfo(ModelSerializer):
_name = "country.info"
_model = "res.country"
_model_fields = ["code", "name"]
def get_odoo_record(self):
if self.code:
return self.env[self._model].search([("code", "=", self.code)])
return super().get_odoo_record()</code>
</pre>
<a name="changelog"></a>
<h2><a class="toc-backref" href="#id7">Changelog</a></h2>
<a name="id1"></a>
<h3><a class="toc-backref" href="#id8">14.0.1.0.0</a></h3>
<p>First official version.</p>
<a name="id2"></a>
<h3><a class="toc-backref" href="#id9">15.0.1.0.0</a></h3>
<p>Second version.</p>
<a name="bug-tracker"></a>
<h2><a class="toc-backref" href="#id10">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:%20model_serializer%0Aversion:%2015.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="#id11">Credits</a></h2>
<a name="authors"></a>
<h3><a class="toc-backref" href="#id12">Authors</a></h3>
<ul class="simple">
<li>Wakari</li>
</ul>
<a name="contributors"></a>
<h3><a class="toc-backref" href="#id13">Contributors</a></h3>
<ul class="simple">
<li>François Degrave <<a class="reference external" href="mailto:f.degrave@wakari.be">f.degrave@wakari.be</a>></li>
</ul>
<a name="maintainers"></a>
<h3><a class="toc-backref" href="#id14">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/fdegrave"><img alt="fdegrave" src="https://github.com/fdegrave.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/15.0/model_serializer">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>