Robert H.
Robert H.

Reputation: 31

Odoo: Check if a module is installed

I want to check if a module in odoo13 is installed. When I print an invoice I want to check in that moment. This is the code i have so far.

class Moduletest(models.Model):
    _inherit = "account.move"


    module_manufacturer_installed = fields.Boolean(
        compute='_compute_module_manufacturer_installed',
        string='Is Module installed?',
    )

    @api.constrains
    def _compute_module_manufacturer_installed(self):
        print('##############################**************************')
        module_manufacturer_installed = False
        for record in self:
            module = self.env['ir.module.module'].search([
                ('name', '=', 'product_manufacturer')
            ])
            if module and module.state == 'installed':
                record.update({
                    'module_manufacturer_installed': True,
                })

I made a field and want to check the status of the module. The function is working I have tested it in other models so i guess either I am inheriting wrong model here "account.move" or my decorator is not working correct. Can anyone advise. Thanks

Upvotes: 1

Views: 637

Answers (1)

Charif DZ
Charif DZ

Reputation: 14751

For compute fields use api.depends not api.constrains, and when you use depends you should at least call it without any arguments api.depends() if you don't have any dependency on any field:

module_manufacturer_installed = fields.Boolean(
                           compute='_compute_module_manufacturer_installed',
                           string='Is Module installed?')

@api.depends()
def _compute_module_manufacturer_installed(self):
    # don't repeat the same search for every record the result will not be changed
    module = self.env['ir.module.module'].search([
            ('name', '=', 'product_manufacturer')
        ])
    module_manufacturer_installed = True if module and module.state == 'installed' else False
    for record in self:
             # no need for update when you set only one field just use normal asignement 
            record.module_manufacturer_installed = module_manufacturer_installed 

@api.constrains is used to validate the the field value after updating it in the database, if the value is wrong an exception should be raised and this will cause rollback.

Upvotes: 2

Related Questions