I'm working with Odoo 8 and I'm trying to introduce data in the database through a XML file.
The content of this file is the next one:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="event_invitation_email_template" model="email.template">
<field name="name">Event Invitation</field>
<field name="subject">Invitation to an event</field>
<field name="email_from">${(object.event_id.user_id.email or 'noreply@localhost')|safe}</field>
<field name="email_to">${object.email|safe}</field>
</record>
</data>
</openerp>
This works great. But I want to add another field, model_id
, which is a Many2one
(pointing to ir.model
). I would know how to do this if the value I want to set had XML ID, but it is not the case.
I want to set the model my.new.model as the model_id
, but I don't know its ID in database if I don't do a search.
So I have no idea how to manage my purpose. Can anyone help me, please?
Background:
At loading data by installing resources (models, fields, model-records, views, record rules, actions, menu entries, etc.), Odoo registers mapping records for all resources in the 'ir_model_data' table ('ir.model.data' object), so we can reference those records later, even from other modules.
Mapping records in 'ir_model_data' have the following remarkable columns:
The record name in conjunction with the module name form what we know as the resource External ID which has the following fully-qualified form: 'module_name.resource_name'.
Records defined in XML data define their External IDs by means of the 'id' attribute of the 'record' tag.
The 'id' attribute can explicitly include 'module_name.' (fully-qualified form) or not. In the later case (relative form) the module that declares the resource is assumed:
The above long story was required to have enough background for the answer that follows.
Answer:
At processing child 'field' tags of any 'record' tag, the 'ref' attribute is expected to have the External ID of a referenced existing record (fully-qualified or relative form):
Mapping records that reference models are created at initializing modules/models/fields. As they lack a corresponding XML record and cannot declare its corresponding External ID by this means, the ORM creates its External ID based in the 'model_' prefix and the object's technical name (dots replaced by underscores):
This is why records that reference models ('ir.model' records) have the following form: model_XXXXX
Records for some other resources also have known prefixes for their External IDs:
Modules: https://github.com/odoo/odoo/blob/8.0/openerp/modules/db.py#L90
Models: https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L382
Fields: https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L444
SELECT * FROM "ir_model_data" WHERE name LIKE 'module_%'; -- most are 'ir.module.module' records
SELECT * FROM "ir_model_data" WHERE name LIKE 'model_%' ORDER by module, name; -- all/most are 'ir.model' records
SELECT * FROM "ir_model_data" WHERE name LIKE 'field_%' ORDER BY module, name; -- all/most are 'ir.model.fields' records
Records for other resources may follow some convention or simply not:
SELECT * FROM "ir_model_data" WHERE name LIKE 'view_%' ORDER by module, name; -- all/most are 'ir.ui.view' records
SELECT * FROM "ir_model_data" WHERE model LIKE 'ir.ui.view' AND name NOT LIKE 'view_%' ORDER by module, name; -- 'ir.ui.view' records with unconventional names
So in your case, the line you're looking for is:
<field name="model_id" eval="ref('your_module.model_my_new_model')"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With