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.
Declare a field
Section titled “Declare a field”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 withBlob::from(vec). Empty content is no value. max_widthandmax_heightare pixels. Leave one out, or give0, 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. Animagefield 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)).
Write and read
Section titled “Write and read”| Surface | Write | Read |
|---|---|---|
Typed Rust (Vals, RecordSet::get) | Option<Blob>, Vec<u8> or &[u8] | Option<Blob> |
| Dynamic values and RPC | standard padded base64 text; false clears the value | the bytes; base64 text over RPC |
- The dynamic write also accepts a map with a
contentkey 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_sizetrue 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 example4.88 Kb), andfalsefor an empty value.read,search_read,web_read,web_search_readandweb_saveanswer the same way. The web client reads its views like this: see Views andbin_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.
Views and bin_size
Section titled “Views and bin_size”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
falsewhen it is empty. - The
imagewidget reads the picture it draws itself, with onereadof that field withoutbin_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. Withoptions="{'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:
onchangeandweb_saveleave 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. onchangeanddefault_getare called withoutbin_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.
What an image field checks
Section titled “What an image field checks”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_permitspictures at once on a node. A write waits for a free decode and for its result no longer thanbase.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 imageInvalid image for res.partner.image_1920: SVG and other markup is not accepted; use PNG, JPEG, GIF or WebPA 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 againStored sizes
Section titled “Stored sizes”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.
The image mixin
Section titled “The image mixin”Addon base ships the abstract model image.mixin (erp_addon_base::ImageMixin), the same set of
fields as Odoo’s image.mixin:
| Field | Bounds | Kind |
|---|---|---|
image_1920 | 1920 x 1920 | the picture you write |
image_1024 | 1024 x 1024 | stored size of image_1920 |
image_512 | 512 x 512 | stored size of image_1920 |
image_256 | 256 x 256 | stored size of image_1920 |
image_128 | 128 x 128 | stored 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.
System parameters
Section titled “System parameters”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.
| Parameter | Meaning | Default | Floor | Hard bound |
|---|---|---|---|---|
base.image_max_pixels | the most pixels (width x height) an image field accepts | 40,000,000 | 1,000,000 | 100,000,000 |
base.image_max_bytes | the most bytes of a picture an image field accepts | 10,485,760 | 65,536 | 134,217,728 |
base.image_jpeg_quality | the quality of a JPEG the server encodes | 95 | 1 | 100 |
base.image_decode_timeout_secs | how long a write waits for a free decode and its result, in seconds | 10 | 1 | 120 |
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:
| Key | Meaning | Default | Floor | Hard bound |
|---|---|---|---|---|
attachments.image_decode_permits | picture decodes one node runs at once | 2 | 1 | 64 |
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.
Limits of this slice
Section titled “Limits of this slice”- 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_bytesand the permits) is not built: the pixel limit isbase.image_max_pixelsalone. bin_sizeasks 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
createandwrite. 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.