콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
7014 화면

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?

아바타
취소
작성자 베스트 답변

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!
아바타
취소
베스트 답변


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.


아바타
취소
베스트 답변

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.

아바타
취소