I try to add a new configuration field for sales configuration settings by inheriting the standard sales settings view/model using a custom module.
Creating the field in the model works fine and a value entered in the view is also successfully written to the database. However, if I reload the view for sales configuration, the previously stored value is back to 0.00 (while still being ok in the table!).
Struggling with this for days now, found related posts by research (odoo site and stackoverflow) which all unfortunately did not work for me.
This is the view definition XML file:
<openerp> <data> <!-- SALE CONFIG FORM VIEW Section --> <record model="ir.ui.view" id="view_custom_sale_config_form_inherited"> <field name="name">sale settings</field> <field name="model">sale.config.settings</field> <field name="type">form</field> <field name="inherit_id" ref="sale.view_sales_config" /> <field name="arch" type="xml"> <data> <xpath expr="//div[@name='Sale Features']/div" position="inside"> <div name="threshold_price_mgr_sig"> <label for="threshold_price_mgr_sig"/> <field name="threshold_price_mgr_sig" class="oe_inline"/> </div> </xpath> </data> </field> </record> </data> </openerp>
This is the Python code:
from openerp.osv import osv, fields import openerp.addons.decimal_precision as dp # modify class sale.config.settings to insert a new field class my_custom_sale_config_settings(osv.osv_memory): _name = 'sale.config.settings' _inherit = "sale.config.settings" _columns = { 'threshold_price_mgr_sig': fields.float('Threshold Price for Managers Signature', digits_compute=dp.get_precision('Product Price'), default_model='sale.config.settings', help="""Threshold price from which the managers signature is required. This is valid for quotations and order confirmations."""), } _defaults = { 'threshold_price_mgr_sig': 0.00, } def get_default_threshold_price_mgr_sig(self, cr, uid, fields, context=None): last_ids = self.pool.get('sale.config.settings').search(cr, uid, [], order='id desc', limit=1) result_obj = self.pool.get('sale.config.settings').browse(cr,uid,last_ids[0]) t_p_m_s = result_obj.threshold_price_mgr_sig return {'threshold_price_mgr_sig': t_p_m_s}
By debugging I also found, that the function get_default_threshold_price_mgr_sig
is never executed…
I know that configuration settings fields works different than normal fields – just can’t figure out how.
Do you know, what I am doing wrong?
Advertisement
Answer
Base configuration wizard for application settings. It provides support for setting default values, assigning groups to employee users, and installing modules. To make such a ‘settings’ wizard, define a model like::
class my_config_wizard(osv.osv_memory): _name = 'my.settings' _inherit = 'res.config.settings' _columns = { 'default_foo': fields.type(..., default_model='my.model'), 'group_bar': fields.boolean(..., group='base.group_user', implied_group='my.group'), 'module_baz': fields.boolean(...), 'other_field': fields.type(...), }
The method execute provides some support based on a naming convention:
* For a field like 'default_XXX', ``execute`` sets the (global) default value of the field 'XXX' in the model named by ``default_model`` to the field's value. * For a boolean field like 'group_XXX', ``execute`` adds/removes 'implied_group' to/from the implied groups of 'group', depending on the field's value. By default 'group' is the group Employee. Groups are given by their xml id. * For a boolean field like 'module_XXX', ``execute`` triggers the immediate installation of the module named 'XXX' if the field has value ``True``. * For the other fields, the method ``execute`` invokes all methods with a name that starts with 'set_'; such methods can be defined to implement the effect of those fields.
The method get_default
retrieves values that reflect the current status of the
fields like ‘default_XXX’, ‘group_XXX’ and ‘module_XXX’. It also invokes all methods
with a name that starts with ‘get_default_’; such methods can be defined to provide
current values for other fields.
The config page works because when you open the configuration page all the “get_” functions will be called. And when you save it all the “set_” functions are going to run.