In Odoo 19, a binary field is used to store files in the database. These files can be images, documents, PDFs, or any other binary content. One of the most common use cases for binary fields is storing company logos, product images, or attachments directly on a record.
For example, you may want to store and display a Company Logo on your model, either uploaded by the user or fetched from a related field.
How to Define a Binary Field
Binary fields are defined in your Python models using fields.Binary. Since they deal with file data, they often come with attributes like string (label), related, default, and readonly.
Example: Basic Binary Field
from odoo import models, fields
class CompanySettings(models.Model):
_name = "company.settings"
_description = "Company Settings"
logo = fields.Binary(related='partner_id.image_1920', default=_get_logo, string="Company Logo", readonly=False,attachment=False)
Here:
- logo - is the field name.
- Binary - tells Odoo this field will store binary data (like an image).
- string - the label shown in the form view.
Useful Attributes of Binary Fields
Binary fields come with flexible attributes to manage file storage and display:
- string - The label shown in the UI.
- related - Points to another field on a related model.
- default - Automatically sets a default image/file.
- readonly - Makes the field editable or not.
- attachment - By default, large binary data is stored in the ir.attachment table to avoid database bloat.
- compute Makes the field dynamically computed instead of stored.
- store(with compute) - Saves the computed value in the database.
- exportable - Excludes the field from export lists.
Displaying Binary Fields in Views
To display binary fields in form views, you use the tag. You can also use widgets to control how they appear.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Form view for Company Settings model -->
<record id="view_company_settings_form" model="ir.ui.view">
<field name="name">company.settings.form</field>
<field name="model">company.settings</field>
<field name="arch" type="xml">
<form string="Company Settings">
<sheet>
<group>
<!-- Binary field with image widget -->
<field name="logo" widget="image" class="oe_avatar"/>
<!-- Binary field with file upload -->
<field name="document_file" filename="document_name"/>
<field name="document_name" invisible="1"/>
</group>
</sheet>
</form>
</field>
</record>
<!-- Action -->
<record id="action_company_settings" model="ir.actions.act_window">
<field name="name">Company Settings</field>
<field name="res_model">company.settings</field>
<field name="view_mode">form,tree</field>
</record>
<!-- Menu Item -->
<menuitem id="menu_company_settings_root"
name="Company Settings"
action="action_company_settings"
parent="base.menu_custom"/>
</odoo>
Example: Image Widget
<field name="logo" widget="image" class="oe_avatar"/>
widget="image" - Renders the binary field as an image.
class="oe_avatar" - Styles the image neatly.
Example: File Upload Widget
<field name="document_file" filename="document_name"/>
<field name="document_name" invisible="1"/>
Here, document_file is the binary field storing the file, and document_name is a Char field storing the filename.
Why Use Binary Fields?
- They allow storing images, logos, and files directly in the system.
- They integrate well with Odoo’s reporting (e.g., print company logo on PDF reports).
- They provide a clean way to manage files per record.
- They support both inline display (as images) and downloadable attachments.
Conclusion
Binary fields in Odoo 19 are essential when working with files, images, and documents. Whether you’re defining a simple logo field, linking to partner images, or managing attachments, fields.Binary gives you the flexibility to handle file data efficiently. By mastering attributes like related, default, readonly, and widgets in views, you can make your Odoo modules more user-friendly and powerful.
To read more about How to Add a Computed Field in Odoo 19, refer to our blog How to Add a Computed Field in Odoo 19.