Skip to Content
Menu
This question has been flagged
1 Reply
3876 Views

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 modelsfields
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 modelsfieldsapi
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
  )


 

Avatar
Discard
Best Answer

You can define the many2one field like this

other_model_id = fields.Many2one(comodel_name = 'model.other', default = lambda self: self._default_other() )

and then add this function

@api.model

def _default_other(self):

    return self.env.ref('other.model.xml_id').id

Avatar
Discard
Author

Nope, it is not working either, it's raising no record found (which I've got as well in some of my tests):

`raise ValueError('No record found for unique ID %s. It may have been deleted.' % (xmlid))`

The point is that I don't see in the log any load event of the data.xml with the records of the dependent model before the error is raised

Maybe your database already has some records in the extendend.model table ?

If so, Odoo loading the module tries to add the column and set the default value to all the existing rows, before loading the xml data files, and so you get the error.

The simplest way to overcome this is to modify the default method to avoid throwing an error if the xml_id is not found, i.e.

rec=self.env.ref('other.model.xml_id', raise_if_not_found=False)

return rec.id if rec else False

and manually set the default value in the column of existing rows just after installing the module i.e.

self.env['extended.model'].search([]).write({'extendend_field': elf.env.ref('other.model.xml_id').id})

Author

Yes, my problems were actually 2:

- having already records in the DataBase, and I've used a validation check in the default function

- I had those fields marked as required on model, which was raising error by setting NULL as default value for existing records

So what I did:

- I've moved required form the model to the view

- I've checked in the default function if I have data or not (I was already doing so)

And for the last part, where you mentioned to manually set the default for the existing records after the module is installed:

- I guess you meant to actually run that code manually (using Odoo shell or custom script)

- but I will try to automatically call that from dependent data XML using function, so it will be done automatically after the data will be loaded (I hope it will work, I'll get there later today)

Your initial answer was inspiring in another way, meaning that you gave me the idea to use a lambda filter which actually calls a function (with parameters)

I did use lambda and separate function (without parameters) for fields default and domain, but haven't thought to combine the two in order to be able to use parameters :)

And that's nice as I have in there quite a few fields with couple of separate functions, and now I can build only one function and pass parameters through lambda

Thank you very much for your help !

Author

For setting default values for existing records a better approach it will be using a post_init_hook actually, instead of view function (which should work as well, but more dirty)

Related Posts Replies Views Activity
1
Jul 22
7368
1
Oct 24
20481
1
Sep 23
1730
1
Sep 21
3341
1
Nov 24
1482