Package: odoo-addon-model-serializer
This module takes advantage of the concepts introduced in thedatamodelmodule to offer mechanisms similar to
(a subset of) theModelSerializerin Django REST Framework. That is, use the definition of the Odoo model to
partially automate the definition of a correspondingDatamodelclass.ImportantThis 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.More details on development statusTable of contentsUsageBasic usageOverriding fields definition(De)serializationChangelog14.0.1.0.015.0.1.0.0Bug TrackerCreditsAuthorsContributorsMaintainersUsageModelSerializerclassTheModelSerializerclass inherits from theDatamodelclass and adds functionalities. Therefore any
class inheriting fromModelSerializercan be used the exact same way as any otherDatamodel.Basic usageHere is a basic example:from odoo.addons.model_serializer.core import ModelSerializer

class PartnerInfo(ModelSerializer):
    _name = "partner.info"
    _model = "res.partner"
    _model_fields = ["id", "name", "country_id"]The result is equivalent to the followingDatamodelclasses: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)Overriding fields definitionIt is possible to override the default definition of fields as such: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"]In this example, we override aNestedModelbut it works the same for any other field type.(De)serializationModelSerializerdoes all the heavy-lifting of transforming aDatamodelinstance into the correspondingrecordset, and vice-versa.To transform a recordset into a (list of)ModelSerializerinstance(s) (serialization), do the following:partner_info = self.env.datamodels["partner.info"].from_recordset(partner)This will return a single instance; if your recordset contains more than one record, you can get a list of instances
by passingmany=Trueto this method.To transform aModelSerializerinstance into a recordset (de-serialization), do the following:partner = partner_info.to_recordset()Unless an existing partner can be found (see below), this methodcreates a new recordin the database. You can avoid
that by passingcreate=False, in which case the system will only create them in memory (NewIdrecordset).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 theidfield of the instance corresponds to a database record. This default behavior can be
modified like so: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()Changelog14.0.1.0.0First official version.15.0.1.0.0Second version.Bug TrackerBugs are tracked onGitHub 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 welcomedfeedback.Do not contact contributors directly about support or help with technical issues.CreditsAuthorsWakariContributorsFrançois Degrave <f.degrave@wakari.be>MaintainersThis module is maintained by the OCA.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.Currentmaintainer:This module is part of theOCA/rest-frameworkproject on GitHub.You are welcome to contribute. To learn how please visithttps://odoo-community.org/page/Contribute.