Search code examples
pythonodooodoo-13

Odoo default data for binary field


I'm developing a custom module in Odoo 13 CE. I've declared a model that contains a binary image field like this:

class allergenCategories(models.Model):
    _name = "mrp_nutrition.allergen.categories"
    _description = "Categorías de alérgenos"
    name = fields.Char("Nombre", required=True)
    image = fields.Binary("Icono de alérgeno") 

So it's a simple class that only contains a name field and an image field. I know how to preload default values in other models to skip the manually creation of most common values by using a data.xml file, but this file can (as far as I know) only store text values for the records.

I guess there's a way to call a .png file to be loaded from the module folders directly but I don't know how to achieve it and I can't find any further information from Odoo's documentation nor Stack Overflow's questions.


Solution

  • From the Data Files documentation:

    type
    if a type attribute is provided, it is used to interpret and convert the field’s content.
    The field’s content can be provided through an external file using the file attribute, or through the node’s body.

    You can set the type and file to load like following:

    <field name="image" type="base64" file="base/static/img/country_flags/ad.png"/>
    

    The above example is taken from the base/data/res_country_data.xml file