Skip to main content

Model Serializer

Alpha License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runbot

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.

Table of contents

Usage

ModelSerializer class

The ModelSerializer class inherits from the Datamodel class and adds functionalities. Therefore any class inheriting from ModelSerializer can be used the exact same way as any other 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 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 NestedModel but it works the same for any other field type.

(De)serialization

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

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

To transform a 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 create=False, in which case the system will only create them in memory (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 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()

Changelog

14.0.1.0.0

First official version.

Bug Tracker

Bugs are tracked on GitHub Issues. 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 feedback.

Do not contact contributors directly about support or help with technical issues.

Credits

Authors

  • Wakari

Contributors

Maintainers

This module is maintained by the OCA.

Odoo Community Association

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.

Current maintainer:

fdegrave

This module is part of the OCA/rest-framework project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

Metadata

Release files for odoo14-addon-model-serializer 14.0.1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for odoo14-addon-model-serializer 14.0.1.0.1
File Interpreter ABI Platform Actions
odoo14_addon_model_serializer-14.0.1.0.1-py3-none-any.whl Python 3 none any Details for odoo14_addon_model_serializer-14.0.1.0.1-py3-none-any.whl

Release files / odoo14_addon_model_serializer-14.0.1.0.1-py3-none-any.whl

Download URL odoo14_addon_model_serializer-14.0.1.0.1-py3-none-any.whl
Size 32.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3de9a1e7d7937999dc583d5ed4b7339339594e1ec88d875b96eff0e51815596a
BLAKE2b-256 checksum
How to use checksums
5e94bb08291863c1c23ba24c9b92b258a7c424c601032ad2ce86e31baea596b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.5

Security for odoo14-addon-model-serializer 14.0.1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page