Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
31895 Vistas

Dear all,

I am having a problem on Odoo v12 on saving data on "res.config.settings" model.

I have inherited the model and defined my custom fields:

       class ResConfigSettings(models.TransientModel):
           _inherit = ['res.config.settings']
           #My custom fields
           my_custom_field1_id = fields.Many2one('res.partner', string='For Customer')
           my_custom_field2_id = fields.Many2one('res.partner', string='For Supplier')

The above code is the only code I have on the module .py.

For the UI I have inherited the "res_config_settings_view_form" on the corresponding XML file.

      <record id="res_config_settings_mymodule_form" model="ir.ui.view">
      <field name="name">Config Settings for My Module</field>
      <field name="model">res.config.settings</field>
      <field name="priority" eval="90"/>
      <field name="inherit_id" ref="base.res_config_settings_view_form"/>
      <field name="arch" type="xml">
           <xpath expr="//div[hasclass('settings')]" position="inside">
           <div class="app_settings_block" data-string="MyModule" string="MyModule" data-key="mymodule" groups="account.group_account_manager">
           <t groups="account.group_account_user">
                <h2>My Module Defaults</h2>
                <div class="row mt16 o_settings_container">
                <div class="col-12 col-lg-6 o_setting_box" title="These values are default values the module">
                <div class="o_setting_left_pane">
                </div>
                     <div class="o_setting_right_pane">
                     <span class="o_form_label">Default Data</span>
                     <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." aria-label="Values set here are company-specific." groups="base.group_multi_company" role="img"/>
                     <div class="text-muted">
                     Default values for module
                     </div>
                     <div class="content-group">
                     <div class="row mt16">
                          <label string="Customer" for="my_custom_field1_id" class="col-lg-3 o_light_label"/>
                          <field name="my_custom_field1_id" domain="[('customer', '=', 'True'), ('company_id', '=', company_id)]"/>
                     </div>
                     <div class="row mt16">
                          <label string="Supplier" for="my_custom_field2_id" class="col-lg-3 o_light_label"/>
                          <field name="my_custom_field2_id" domain="[('supplier', '=', 'True'), ('company_id', '=', company_id)]"/>
                     </div>
                     </div>
                     </div>
                     </div>
                     </div>
           </t>
           </div>
           </xpath>
      </field>
      </record>

Everything seems to be working fine and I get no errors and I can see the new "app_settings_block" created for My Module with both new fields and they work for the domain I set for each field.

The problem I have is that when I set a value to any of the fields and click on the "Save" button, the data is simple not saved into the database.

I am sure I am working with correct user/group and have full access to the system.

What could be the problem?

Any idea?

Thank you all in advance

Best regards

Paulo


Avatar
Descartar
Mejor respuesta

Hello Paulo,

The General Settings (res.config.settings) data is not directly save on Save button click, you need to set setter-getter method to manage this field's data. These data are store in config parameter and fetch from this config parameter.

You are doing all perfect, but add bellow code in your python file:

from odoo import api, fields, models, modules

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

    #My custom fields
    my_custom_field1_id = fields.Many2one('res.partner', string='For Customer')
    my_custom_field2_id = fields.Many2one('res.partner', string='For Supplier')

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        res.update(
            my_custom_field1_id = self.env['ir.config_parameter'].sudo().get_param('your_custom_module_name.my_custom_field1_id'),
            my_custom_field2_id = self.env['ir.config_parameter'].sudo().get_param('your_custom_module_name.my_custom_field2_id'),
        )
        return res

    @api.multi
    def set_values(self):
        super(ResConfigSettings, self).set_values()
        param = self.env['ir.config_parameter'].sudo()

        field1 = self.my_custom_field1_id and self.my_custom_field1_id.id or False
        field2 = self.my_custom_field2_id and self.my_custom_field2_id.id or False

        param.set_param('your_custom_module_name.my_custom_field1_id', field1)
        param.set_param('your_custom_module_name.my_custom_field2_id', field2)


Hope it will helpful for you.

Thanks and regards

Haresh Kansara

Avatar
Descartar
Autor

Dear @Haresh,

Thank you very much for your asnwer.

It helps me to better understand how "res.config.settings" works.

Anyway, I'm having a problem with the "set_values()" function you provided on your sample code and need your help on fixing it.

I am testing your code with only "my_custom_field1" from the code on my question.

After testing it, only with the "get_values()" function, I can see on the postgresql database table "res_config_setting" that the ID for the selected partner is correctly set.

In my case, the id is "7".

When I "activate" the "set_values()" function, I am getting the below error:

"Database fetch misses ids (('7',)) and has extra ids ((7,)), may be caused by a type incoherence in a previous request"

In fact this ids "7" from the error is the same id for the selected partner and I believe it's something related with data type.

Tested with other partners and the error id is always equal to the id of the selected partner.

Can you please help fixe this?

Thank you in advance

Best regards

Paulo

Autor

Dear @Haresh,,

I have found the issue.

I had to convert the result into an integer since it receives a char result.

I have changed the code for "get_values()" function to:

my_custom_field1_id = int(self.env['ir.config_parameter'].sudo().get_param('your_custom_module_name.my_custom_field1_id')),

By convert the result to an integer (int()) value it's working as expected.

Thank you once again

Best regards

Paulo

Excellent solution, works great for me!

Thanks a lot

Mejor respuesta

editor doesn't save the content when I press "Save" odoo14???????


Avatar
Descartar

field_name = fields.Integer('Field String',config_parameter="field_name")

Add config_parameter this parameter in the field definition

Publicaciones relacionadas Respuestas Vistas Actividad
0
mar 15
3530
2
jul 24
8264
2
abr 25
2785
5
mar 25
19059
1
jun 24
1882