Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
6921 Vizualizări

Hi! 

I seem to be in a struggle to find the way to achieve one simple task. So far, reading the sources of stock modules, I have created a module for res_config_settings:

# -*- coding: utf-8 -*-

from odoo import fields, models
from decimal import Decimal


class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

exchange_rate = fields.Float('USD/RUB', digits=(2, 2), default=69.0)
tid_en = fields.Char('Tracking ID for EN', default='UA-XXXXXXXX-1')
tid_ru = fields.Char('Tracking ID for RU', default='UA-XXXXXXXX-3')

def get_values(self):
res = super(ResConfigSettings, self).get_values()
res.update(
exchange_rate=self.env['ir.config_parameter'].sudo().get_param('crm_patch.exchange_rate'),
tid_en=self.env['ir.config_parameter'].sudo().get_param('crm_patch.tid_en'),
tid_ru=self.env['ir.config_parameter'].sudo().get_param('crm_patch.tid_ru')
)
return res

def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('crm_patch.exchange_rate',
Decimal(self.exchange_rate))
self.env['ir.config_parameter'].sudo().set_param('crm_patch.tid_en', self.tid_en)
self.env['ir.config_parameter'].sudo().set_param('crm_patch.tid_ru', self.tid_ru)

What am I trying to achieve is to have these defaults in another module:

def _get_tid(self, x_site_lang):
if x_site_lang == 'ru':
return self.env['ir.config_parameter'].sudo().get_param('crm_patch.tid_ru')
else:
return self.env['ir.config_parameter'].sudo().get_param('crm_patch.tid_en')


So I guess I have to make some way to set ir.config_parameters on module installation once, if they are not present already?

Could you please help me with the task?

Imagine profil
Abandonează
Autor Cel mai bun răspuns

Thank you for your answers, people! I have managed to solve the problem:

1. I have created a default_settings.xml file with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data noupdate="1">
         <record id="config_crm_patch_exchange_rate" model="ir.config_parameter">
            <field name="key">crm_patch.exchange_rate</field>
            <field name="value">69.0</field>
        </record>
        <record id="config_crm_patch_tid_en" model="ir.config_parameter">
            <field name="key">crm_patch.tid_en</field>
            <field name="value">UA-XXXXXXXX-1</field>
        </record>
        <record id="config_crm_patch_tid_ru" model="ir.config_parameter">
            <field name="key">crm_patch.tid_ru</field>
            <field name="value">UA-XXXXXXXX-3</field>
        </record>
    </data>
</odoo>
2. Added the file to manifest, in the 'data' section:
...
    'data': [
        'views/crm_lead.xml',
        'views/crm_opportunity.xml',
        'views/res_config_settings.xml',
        'data/default_settings.xml'
    ],
...

Turns out it's as simple as that!
Imagine profil
Abandonează
Cel mai bun răspuns


Your code is perfect, I dont see any issue, however the questions is when did you add the default property? Did you by chance add the default-property after installing the module, i.e in other words did you add it after the field has been created?

If yes, then likely default w.r.t Config Objects will not be applicable.


Imagine profil
Abandonează
Cel mai bun răspuns

you should create demo data into ir.parameter object for this two parameter : 
crm_patch.tid_en, crm_patch.tid_ru

so when you do get_value and set_value, you will get parameters values.

Imagine profil
Abandonează