Skip to content
ERPForAll DocsDevelopment

    Binary and image fields

    A binary field keeps file content on a record. An image field is a binary field that accepts pictures only and can store them scaled down. Odoo calls them fields.Binary and fields.Image. The design is ADR 0089 (docs/adr/0089-model-binary-field-and-bytea-column-storage.md); this page describes its column-backed mode: the content is stored in a bytea column of the model’s table.

    Status: the field kinds, the image checks, the image mixin and the company logo fields are delivered by task IMG.FIELD. In views, show a binary field with the image widget (an image_url widget shows a char field that holds a URL). A form sends a chosen picture as base64 inside the record’s write, so the request body limit (limits.rpc_body_bytes) bounds it; the widget refuses a larger file and SVG before it reads anything. The Rust examples on this page are not compiled as doctests.

    use erp_sdk::prelude::*;
    #[model(name = "library.book", description = "Book")]
    pub struct Book {
    #[field(char, required = true)]
    pub name: Option<Arc<str>>,
    /// any file
    #[field(binary, string = "Scan")]
    pub scan: Option<Blob>,
    /// a picture, stored at most 1024 x 1024 pixels
    #[field(image, max_width = 1024, max_height = 1024, string = "Cover")]
    pub cover: Option<Blob>,
    }
    • The Rust type is Option<Blob>. Blob (erp_sdk::Blob, in the prelude) is shared, immutable bytes. It dereferences to [u8]; build one with Blob::from(vec). Empty content is no value.
    • max_width and max_height are pixels. Leave one out, or give 0, and that edge has no bound. A bound is between 16 and 4096; a value outside that range refuses the registry and names the field. An image field with no bound at all checks the picture and stores the bytes as they came.
    • Both kinds report the field type binary.
    • The content is not loaded with the other columns of a record. It is read when a caller asks for that field.

    A binary or image field cannot be indexed, translated or made company-dependent: the registry refuses such a declaration and names the field. A search cannot order by it and a grouped read cannot group by it; both are refused by name (Cannot order by the binary field library.book.cover). A domain can still test whether a value is set (('cover', '!=', False)).

    SurfaceWriteRead
    Typed Rust (Vals, RecordSet::get)Option<Blob>, Vec<u8> or &[u8]Option<Blob>
    Dynamic values and RPCstandard padded base64 text; false clears the valuethe bytes; base64 text over RPC
    • The dynamic write also accepts a map with a content key in base64 (the {filename, content} form of Odoo clients). The file name is not stored.
    • Text that is not valid base64 is refused with a message that names the model and field (for example library.book.scan) and the defect (here, a length that is not a multiple of four), never the content.
    • With bin_size true in the context, a read returns the size as text in place of the content (two decimals and a unit out of bytes, Kb, Mb, Gb, Tb, for example 4.88 Kb), and false for an empty value. read, search_read, web_read, web_search_read and web_save answer the same way. The web client reads its views like this: see Views and bin_size.
    • A read that names no fields returns every readable field except binary and image fields. Name the field to read its content.
    • Export writes the content as base64 and import reads base64 back.
    • An addon built for the WASM ABI 1 cannot read or write these fields: a call that names one is refused, as for the other native kinds.

    The web client adds bin_size: true to the record reads of its views: the list and card reads (web_search_read), the form read (web_read) and the answer of a save (web_save). A view therefore never carries file content it does not draw:

    • A binary field in a view arrives as its size text, or false when it is empty.
    • The image widget reads the picture it draws itself, with one read of that field without bin_size (addons/web/static/src/client/binary-content.ts). The cells of one list column, or the cards of one page, travel as one request. With options="{'preview_image': 'image_128'}" the widget draws the smaller field and reads the field itself only when the user enlarges (zoom) or downloads the picture. Name the smallest stored size that fits in lists and cards.
    • Until the picture arrives the widget shows the size; when the view moves on first, the read is cancelled; Download saves through a Blob, shows that it is reading, ignores a second press meanwhile and says when the file was removed; a read that fails leaves the placeholder and says why under the field. A record that is not saved yet has no stored picture to read, and a picture the user just picked is shown from the form itself.
    • A size is never written back: onchange and web_save leave out a binary field whose value is still the size text, so the stored file stays as it is. A picked file travels as base64, and Clear sends an empty value.
    • onchange and default_get are called without bin_size; a binary value they set arrives as content.

    Calls from your own client code are not changed: a read that names a binary field without bin_size in its context answers the content.

    Every write of an image field checks the content, from typed Rust and from RPC alike:

    • Type. The content must be PNG, JPEG, GIF or WebP. The type is read from the bytes, never from a file name or a declared media type. SVG and other markup is refused for every user.
    • Bytes. The content must not be larger than the system parameter base.image_max_bytes.
    • Pixels. Width times height, read from the file header before any pixel is decoded, must not exceed the system parameter base.image_max_pixels. Every frame of a GIF must lie inside the screen its header declares, so no frame is larger than the checked size.
    • Size. A PNG or JPEG larger than the field’s bounds is scaled down to fit them. The aspect ratio is kept and a picture is never enlarged: a 400 x 200 picture in a 128 x 128 field is stored as 128 x 64, and in a 1024 x 1024 field as 400 x 200.
    • Encoding. A PNG or JPEG in a bounded field is decoded, turned upright by its orientation and encoded again in its own format, so the metadata of the uploaded file is not stored. JPEG uses the quality of base.image_jpeg_quality. A JPEG that already fits and carries no metadata is kept as it is.
    • GIF and WebP that fit the field’s bounds are stored as they came, so an animation stays whole. A larger one is decoded (its first frame), scaled down to the bounds and stored as a PNG: a bounded field never holds a picture larger than its bounds. An unbounded field keeps every GIF and WebP as it came.
    • Colour profile. The re-encoded picture carries no ICC profile, as in the reference: the colours of a wide-gamut photograph can shift slightly.
    • Decoding. Decoding and scaling run on the server’s blocking pool, at most attachments.image_decode_permits pictures at once on a node. A write waits for a free decode and for its result no longer than base.image_decode_timeout_secs; then it fails with an error that names the field and the setting, and nothing is stored. A computed or related image field with bounds must be stored (store = true): the registry refuses bounds on one that is not.

    A refused picture gives a validation error that names the field and the reason, for example:

    Invalid image for res.partner.image_1920: the file is not a PNG, JPEG, GIF or WebP image
    Invalid image for res.partner.image_1920: SVG and other markup is not accepted; use PNG, JPEG, GIF or WebP

    A picture above the byte or pixel limit is refused the same way and the message names base.image_max_bytes or base.image_max_pixels. When no decode becomes free in time the error is a resource error:

    res.partner.image_1920: no picture decode became free within 10 s (base.image_decode_timeout_secs; this node decodes 2 pictures at once, attachments.image_decode_permits); try again

    A stored related image field with smaller bounds is a stored size of its source. It is filled from the source when the source is written, and cleared when the source is cleared:

    #[field(image, max_width = 1920, max_height = 1920, string = "Cover")]
    pub cover: Option<Blob>,
    /// the cover at most 256 x 256 pixels, stored
    #[field(image, max_width = 256, max_height = 256, related = "cover", store = true)]
    pub cover_256: Option<Blob>,

    The source can be on another record: res.company.logo_web (at most 180 pixels wide, any height) is a stored size of partner_id.image_1920. Lists and cards read the small field, so the server does not scale a large picture on every read.

    Addon base ships the abstract model image.mixin (erp_addon_base::ImageMixin), the same set of fields as Odoo’s image.mixin:

    FieldBoundsKind
    image_19201920 x 1920the picture you write
    image_10241024 x 1024stored size of image_1920
    image_512512 x 512stored size of image_1920
    image_256256 x 256stored size of image_1920
    image_128128 x 128stored size of image_1920

    Inherit it as an Odoo addon writes _inherit = ['image.mixin']:

    use erp_addon_base::ImageMixin;
    use erp_sdk::prelude::*;
    #[model(name = "library.author", description = "Author", inherit = [ImageMixin])]
    pub struct Author {
    #[field(char, required = true)]
    pub name: Option<Arc<str>>,
    }

    To add the fields to a model of another addon, use #[extend(TheirModel, addon = "my_addon", inherit = [ImageMixin])]. Write image_1920 only; the four sizes follow.

    In base, contacts (res.partner) inherit the mixin. In product, products (product.template) inherit it, and a variant shows its template’s picture unless it has its own (Product pictures). The company logo res.company.logo is a writable related field of the company contact’s image_1920: writing the logo writes the contact’s picture. The server never draws a picture: a contact without one reads false, and there is no generated avatar.

    The limits are declared technical parameters of base (ADR 0227), edited in Technical → System Parameters. A missing or empty value is the default. A value outside the range is refused by name on write, and a stored value outside the range refuses the write of a picture. Zero never means unlimited.

    ParameterMeaningDefaultFloorHard bound
    base.image_max_pixelsthe most pixels (width x height) an image field accepts40,000,0001,000,000100,000,000
    base.image_max_bytesthe most bytes of a picture an image field accepts10,485,76065,536134,217,728
    base.image_jpeg_qualitythe quality of a JPEG the server encodes951100
    base.image_decode_timeout_secshow long a write waits for a free decode and its result, in seconds101120

    The default byte limit is the most a report prints for one picture (its image_bytes limit), so a stored logo always prints. A higher value is allowed; a picture above 10 MiB is then stored but a report that prints it stops with its image_bytes limit.

    The number of decodes a node runs at once is node configuration, in the [attachments] table of the server configuration file, read at start:

    KeyMeaningDefaultFloorHard bound
    attachments.image_decode_permitspicture decodes one node runs at once2164

    The limits are read once per transaction, on the first admission of a picture (create, write, onchange, new(), defaults, upsert and the stored sizes a flush computes). An onchange admits the pictures the form sends. The pictures the edited record already holds in the database start its draft as they are stored: they were admitted when they were saved, are not decoded again on every onchange, and a picture saved under an earlier, wider limit does not stop the form from changing other fields. In Rust the names and bounds are in erp_sdk::image (ImagePolicy::PARAMETERS, DecodePool), next to probe, which returns the format and header size of image content.

    • Storage is the column-backed mode of ADR 0089 only. The attachment-backed mode of that ADR (content in the blob store, read as a handle, upload ids instead of base64) is not built yet.
    • The node rule of ADR 0089 B9 (a pixel cap derived from process.memory_soft_bytes and the permits) is not built: the pixel limit is base.image_max_pixels alone.
    • bin_size asks the database for the length (octet_length) when the transaction holds no value for the records; a value already in the transaction is sized from it. A stored file named in such a read is not fetched, for any user: the record checks are decided without its column. A computed picture is still computed to be sized, so its sources are read.
    • A draft (new) record keeps a picture as it was given; the checks and the scaling run when the record is created or written.
    • The image limits are read from the system parameters by create and write. A recompute of a stored size that runs in a transaction which wrote no picture uses the declared defaults; its source was already checked when it was written.