Skip to content
Home » My Blog Tutorial » Odoo XML: Adding Binary Fields

Odoo XML: Adding Binary Fields

Odoo XML Adding Binary Fields

This tutorial explains how to add binary fields in XML files in Odoo. Furthermore, we cover crucial aspects such as creating records, handling different field types (many2one, many2many, one2many), and managing file uploads. Consequently, you will learn to properly maintain binary fields when creating new records through XML.

Introduction to Binary Fields in Odoo

Binary fields in Odoo store data, such as files or images, in a binary format. Therefore, they are essential for handling non-textual information within your Odoo models. Moreover, when you create records via XML, you must correctly define these fields to ensure data integrity.

Prerequisites

Before you begin, ensure you have the following:

  • An active Odoo installation.
  • Basic knowledge of XML.
  • Access to Odoo’s backend.
  • A custom module.

Step 1: Defining the Binary Field in Your Model

First, you need to define the binary field in your Odoo model. Consequently, this involves editing the Python file associated with your model.

from odoo import models, fields

class SchoolStudent(models.Model):
    _name = 'school.student'
    _description = 'Student Information'

    name = fields.Char(string='Name', required=True)
    student_image = fields.Binary(string='Image')
    file_name = fields.Char(string='File Name')

In this example, student_image is the binary field that will store the image. Additionally, file_name stores the name of the uploaded file.

Step 2: Adding the Binary Field to the XML View

Next, add the binary field to your XML view. Thus, this allows users to interact with the field in the Odoo interface.

<odoo>
    <data>
        <record id="view_school_student_form" model="ir.ui.view">
            <field name="name">school.student.form</field>
            <field name="model">school.student</field>
            <field name="arch" type="xml">
                <form string="Student Form">
                    <sheet>
                        <group>
                            <field name="name"/>
                            <field name="student_image" widget="image" class="oe_avatar"/>
                            <field name="file_name"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <record id="view_school_student_tree" model="ir.ui.view">
            <field name="name">school.student.tree</field>
            <field name="model">school.student</field>
            <field name="arch" type="xml">
                <tree string="Student Tree">
                    <field name="name"/>
                    <field name="student_image"/>
                    <field name="file_name"/>
                </tree>
            </field>
        </record>
    </data>
</odoo>

Here, widget="image" in the form view tells Odoo to display the binary field as an image.

Step 3: Adding Binary Field Data via XML Records

When creating records using XML, you can include binary data. Therefore, you need to provide the file path for the binary field.

<odoo>
    <data>
        <record id="school_student_1" model="school.student">
            <field name="name">Iron Man</field>
            <field name="student_image" type="base64" file="your_module_name/static/description/iron_man.png"/>
            <field name="file_name">iron_man.png</field>
        </record>
    </data>
</odoo>

  • type="base64": Specifies that the file is encoded in Base64.
  • file: Provides the path to the file within your module.

Step 4: Understanding the File Path

The file path is crucial for Odoo to locate the image. Consequently, the structure typically follows this pattern:

your_module_name/static/description/your_image.png

  • your_module_name: The name of your custom module.
  • static/description: The standard directory for storing static files like images.
  • your_image.png: The name of your image file.

Step 5: Upgrading Your Module

After making changes, upgrade your module. Thus, this ensures that Odoo registers the new fields and views. You can upgrade through the Odoo Apps menu or via the command line:

./odoo-bin -c /path/to/your/odoo.conf -u your_module_name

Step 6: Handling File Names

It’s good practice to store the file name separately. Therefore, you can use a Char field to keep track of the original file name.

<field name="file_name">iron_man.png</field>

Troubleshooting

  • File Not Found: Double-check the file path in your XML record.
  • Image Not Displaying: Ensure you’ve used the correct widget in the form view (widget="image").
  • Upgrade Issues: If changes don’t appear, try restarting your Odoo server.

Advanced Usage: Using Binary Fields in CSV Files

You can also create records with binary fields using CSV files. However, this requires encoding the binary data in Base64 within the CSV.

Conclusion

Adding binary fields in XML files in Odoo involves several steps. By following this tutorial, you should now understand how to define binary fields, add them to views, and include binary data in XML records. Remember to always double-check your file paths and upgrade your module after making changes.

Further Learning


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Tags:

Leave a Reply

Optimized by Optimole
WP Twitter Auto Publish Powered By : XYZScripts.com

Discover more from teguhteja.id

Subscribe now to keep reading and get access to the full archive.

Continue reading