ช่วยเหลือ

0

Odoo10: Cannot add a record to res.partner using a data file

Avatar
Antonio Miras

I have created a straight forward addon using Odoo bult-in scaffolding tool.

I'm trying to add a record to res.partner using a data file, like this

<odoo>
<data>
<record model="res.partner" id="NewPartner">
<field name="parent_id" eval="ref('base.partner_root')"/>
<field name="name">New Partner</field>
<field name="supplier" eval="True"/>
<field name="employee" eval="False"/>
<field name="is_company" eval="True"/>
<field name="customer" eval="False"/>
<field name="sale_warn">no-message</field>
</record>
</data>
</odoo>

But I keep getting the following error, no matter how I populate "sale_warn"

ParseError: "null value in column "sale_warn" violates not-null constraint

Indeed, sale_warn is a required field, so I need to set a value, but I can't see how.

Furthermore, there are several working AddOns adding records to res.partner without declaring sale_warn field.


 

Avatar
Discard
1 Answer
0
Avatar
Antonio Miras
Best Answer

The problem:

My AddOn relies on Sales module.

When Odoo reads my XML data file and tries to insert a new record into res.partner, the model being used is the stock one. To clarify, the one living in “odoo/odoo/addons/base/res/res_partner.py” which doesn't have a sale_warn field.

Now, as Sales module's been installed, the res_partner table in the database already has several required fields found in the inherited ResPartner model. The one in “odoo/addons/sale/models/res_partner.py”

So when the upgrade process is running, no matter whether I use a data file or a Python function, I can't access the fields from the inherited model, but the database runtime is waiting to throw an exception if I don't write those fields.

The solution:

After Odoo is done loading modules, the model res.partner available in the Environment object is the proper one (ResPartner), with all required fields available.

So, in the end, my solution is:

1) Call a python function in my data.xml that will activate a flag, to signal whether my Addon has been installed or upgraded

2) As soon as any part of my AddOn takes over, check the flag, and run an python function to insert default records. A good place to do that might be overriding fields_view_get in any model.

You can also use an additional xml data (not present in manifest.py, obiously), parsed in Python code to insert the data.

Avatar
Discard