OdooWiki OCA HUB

Model Serializer

model_serializer folder_open OCA/rest-framework open_in_new
Uncategorised License: LGPL-3

This module takes advantage of the concepts introduced in the datamodel module to offer mechanisms similar to (a subset of) the ModelSerializer in Django REST Framework. That is, use the definition of the Odoo model to partially automate the definition of a corresponding Datamodel class.

auto_awesome What this module does

AI-generated from the code · Odoo 15.0

This module lets developers automatically turn Odoo models into serializable data structures, making it easier to send and receive data through APIs or integrations.

This module is a technical tool for developers. It automatically creates data serializers from Odoo models, so that records can be converted to and from simple data formats (like JSON) without writing all the conversion code by hand.

  • It works with the datamodel module and uses the marshmallow library.
  • When a developer defines a serializer class, the module reads the Odoo model definition and generates the corresponding fields automatically.
  • It supports common field types such as text, numbers, dates, booleans, binary data, and relationships (many2one, one2many, many2many).
  • It provides methods to convert a set of Odoo records into serialized objects (from_recordset) and to convert serialized objects back into Odoo records (to_recordset).
  • It includes a minimal serializer (generic.minimal.serializer) that only exposes the record ID and display name, useful for simple references.
  • It adds validation to ensure that nested serializers exist and that required attributes are defined.
  • It extends the datamodel.builder to load and build these serializers when the module is loaded.

There are no menus or user interface elements; this module is used programmatically by other modules or custom code. It touches the base model and introduces new abstract models for serialization.

Depends on

Models and fields (4)
  • base · extends an existing model
  • datamodel.builder · extends an existing model
  • generic.minimal.serializer · new modelid, display_name
  • generic.abstract.serializer · new model

Text generated by AI (DeepSeek) from reading the Odoo 15.0 code on GitHub; it may contain mistakes, so check the code before deciding. View the code on GitHub

Availability by version

Reviewed: 2026-11-03
Confirmed (verified in repository and branch) Estimated (unverified) No information

Confirmation status: 3 confirmed, 6 with no information of 9 versions.

Sources · Odoo 15.0

Confirmed on 2026-11-03 in branch 15.0 of OCA/rest-framework.

terminal Installation and usage

OCA documentation · Odoo 15.0

ModelSerializer class

The :code:`ModelSerializer` class inherits from the :code:`Datamodel` class and adds functionalities. Therefore any
class inheriting from :code:`ModelSerializer` can be used the exact same way as any other :code:`Datamodel`.

Basic usage
***********

Here 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 following :code:`Datamodel` classes::

    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 definition
****************************

It 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 a :code:`NestedModel` but it works the same for any other field type.

(De)serialization
*****************

:code:`ModelSerializer` does all the heavy-lifting of transforming a :code:`Datamodel` instance into the corresponding
:code:`recordset`, and vice-versa.

To transform a recordset into a (list of) :code:`ModelSerializer` instance(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 passing :code:`many=True` to this method.


To transform a :code:`ModelSerializer` instance into a recordset (de-serialization), do the following::

    partner = partner_info.to_recordset()

Unless an existing partner can be found (see below), this method **creates a new record** in the database. You can avoid
that by passing :code:`create=False`, in which case the system will only create them in memory (:code:`NewId` recordset).

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:`id` field 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()

No verified information in OCA: Installation, Configuration.

info

Data imported from GitHub: this sheet reflects the latest catalog review, not a live lookup. Always check the module documentation and staging before installing in production.