I develop one custom module which contains:
1. one custom model (and it's data defined in XML)
2. one model which extends an existing one by adding a Many2one required field pointing to the custom model above
If I try to configure default value for the required Many2one field it raises constraints errors during installation as by that time the XML data of the dependent model wasn't yet loaded (database already contains values in the existing default model which I extend at point 2, without the new field of course)
The dependent model it's imported first in __init__.py (inside models) but I see it doesn't matter
Of course that by splitting them into two separate modules where I specify the dependency it works, but I'd like to know how can I have them in the same module.
Is there a way to load dependent model XML data before initialising the model which depends on it ?
I've extrapolated a simplified version of what I have
Module structure
my_module
├── __init__.py
├── __manifest__.py
├── data
│ └── my_model_data.xml
└── models
├── __init__.py
├── extended_model.py
└── my_model.py
models/my_model.py
# -*- coding: utf-8 -*-
from odoo import models, fields
class MyModel(models.Model):
_name = 'my_model.category'
my_field = fields.Char('My Field')
data/my_model_data.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="my_external_id" model="my_model.category">
<field name="my_field">weight</field>
</record>
</data>
</odoo>
models/extended_model.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class ExtendedModel(models.Model):
_inherit = ['extended.model']
@api.model
def _get_default_id(self):
return self.env.ref('my_module.my_external_id').id
extended_field = fields.Many2one(
'my_model.category',
'Extended field',
required=True,
default=_get_default_id
)