Enable Dark Mode!
how-to-work-with-binary-fields-in-odoo-20.jpg
By: Fathima Mazlin AM

How to Work with Binary Fields in Odoo 20

Technical Odoo 20 Fields and widgets

Many business applications will require an element of file storage. This may include documents, photographs, certificates, and invoices. Within Odoo 20, there is a Binary field which can be used to store files within your modules and on your forms. Whether you are developing a new module or enhancing an existing business application, having knowledge of Binary fields will enable you to implement a file upload function and then both store and retrieve files in a simple manner.

In this blog post, you will learn how to work with Binary fields in Odoo 20.

What Are Binary Fields in Odoo 20?

Binary is a data type available within Odoo that allows businesses to store/attach and/or retrieve binary information (for example, PDFs, Excel, and more). An ‘Image’ field is also available. This adds extra capabilities to the Binary field, including the ability to manipulate and scale images.

The main difference is:

  • Binary field: Suitable for general-purpose files and documents.
  • Image field: Suitable for images such as profile pictures, product images, and scanned photographs.

As well as text, binary fields can also be used to hold signatures. Again, it is dependent on the relevant widget or application which will allow users to draw their signature.

1. Defining and Managing File Uploads

File fields can be used where there is a requirement to upload and download files such as staff qualifications, invoices, contracts and product information.

from odoo import fields, models

class DocumentRecord(models.Model):
    _name = 'document.record'
    _description = 'Document Record'
    name = fields.Char(string='Document Name')
    file_data = fields.Binary(string='Upload File')
    file_name = fields.Char(string='Filename')

File data is for the document, and the file name is for the name of the document. Insert a Binary field into an XML form view.

<field name="file_data" filename="file_name"/>
<field name="file_name" invisible="1"/>
How to Work with Binary Fields in Odoo 20-cybrosys

2. Defining and Displaying Image Fields

Images can be used for many things, including staff, products, company, and profile images. Odoo has a field.Image, which is a superset of Binary that adds image-related features.

from odoo import fields, models
class EmployeeProfile(models.Model):
    _name = 'employee.profile'
    _description = 'Employee Profile'
    
    name = fields.Char(string='Employee Name', required=True)
    image = fields.Image(string='Profile Image')

Image widget in XML:

<field name="image" widget="image"/>

Used to show an image and to allow users to upload or change it.

How to Work with Binary Fields in Odoo 20-cybrosys

Common use cases for Image fields

Image fields are useful for:

  • Employee and student profile pictures.
  • Images of products.
  • Logo (example - company logo).

For general documents, use fields.Binary rather than fields.Image, because image-specific processing may alter the uploaded image.

3. Defining and Managing Sign Fields

Signing allows a user to sign documents and, for example, approve, deliver and enter into contracts. A signature can be held as an image within a Binary field. Capturing a signature is also a different process from the storage of one.

from odoo import fields, models
class ApprovalRecord(models.Model):
   _name = 'approval.record'
   _description = 'Approval Record'
   name = fields.Char(string='Reference', required=True)
   signature = fields.Binary(string='Signature',attachment=True)

The signature field stores the signature image. The attachment=True setting uses Odoo's attachment-based storage, which is the default for Binary fields. If the signature is already available as an image, it can be displayed using a signature widget:

<field name="signature" widget="signature"/>

The signature widget provides an interactive interface for capturing signatures directly from the form. Users can draw their signature, use the available text-based option, or upload an existing signature image. The captured signature is stored in the Binary field and associated with the business record.

How to Work with Binary Fields in Odoo 20-cybrosys

4. Reading and Updating Binary Content

Binary fields can be accessed and updated through Odoo's ORM. For example, an existing document can be replaced using write():

from odoo.tools import BinaryBytes
 
file_content = b"Updated document content"
record.write({
    'file_data': BinaryBytes(file_content),
    'file_name': 'updated_document.txt',
})

Do not write plain bytes directly into a Binary field. Assigning raw bytes such as file_content to file_data raises a TypeError.

To check whether a file exists:

if record.file_data:
    # File content is available.
    pass

To remove the file:

record.write({
    'file_data': False,
    'file_name': False,
})

When updating unrelated fields, avoid assigning an empty value to the Binary field unless the intention is to remove the file. In Odoo 20, reading a Binary field returns a BinaryValue object rather than a base64 string directly. This object provides methods to access the file content, decode text, retrieve base64 data, and obtain file metadata.

For example, an existing document can be read as follows:

val = record.file_data
if val:
    raw_bytes = val.content
    text = val.decode()
    b64 = val.to_base64()
    size = val.size
    mime = val.mimetype

Here:

  • content returns the raw file content as bytes.
  • decode() converts the content into a string.
  • to_base64() returns the base64 representation.
  • size provides the file size in bytes.
  • mimetype provides the file's MIME type.

5. Reading with read() and RPC

record.read() and RPC calls return one dictionary per record. Because these dictionaries must be serializable, Binary values appear as base64 text:

records = record.read(['file_name', 'file_data'])
# [{'id': 7, 'file_name': 'odoo-20-binary-fields-doc.docx',
#   'file_data': 'UEsDBBQAAAAIAA...'}]

Over RPC, the same dictionary arrives as JSON, so file_data is a string that the client decodes with base64 before saving the file. The ORM attribute on the record remains a BinaryValue; only the dictionary representation uses base64 text.

6. Useful attributes of fields.Binary

  • string > Sets the field label displayed in the user interface.
  • attachment > When True, stores the binary content as an attachment rather than directly in the model table.
  • required > Makes the field mandatory.
  • readonly > Prevents users from editing the field through the standard UI.
  • compute > Makes the field computed dynamically rather than stored.
  • store (with compute) > Saves the computed value in the database.
  • default > Automatically sets a default image/file.

The Odoo 20 binary fields offer a lot of flexibility for handling documents and images within business applications by implementing such fields like fields.Binary and fields.Image with Binary storage plus signature capability. It will be much easier for developers to handle any type of business application needs with this knowledge.

To read more about How to Work with Binary Fields in Odoo 19, refer to our blog How to Work with Binary Fields in Odoo 19.


Frequently Asked Questions

What is the difference between fields.Binary and fields.Image?

fields.Binary is used for general binary content, such as documents and files, while fields.Image is designed specifically for images and supports image resizing through attributes such as max_width and max_height.

What does attachment=True mean in Odoo 20?

The attachment=True attribute stores binary content using Odoo's attachment mechanism instead of directly in the model's database table. It is the default for fields.Binary.

Can Binary fields be used to store signatures?

Yes. A Binary field can be used to upload a user's signature image. For electronic signature workflows, Odoo's Sign application provides more specialized functionality.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp