sale.config.settings 中自定义字段的值未显示在销售设置视图中

Value of custom field in sale.config.settings is not displayed in sales settings view

我尝试通过使用自定义模块继承标准销售设置 view/model 来为销售配置设置添加新的配置字段。

在模型中创建字段工作正常,在视图中输入的值也成功写入数据库。 但是,如果我重新加载销售配置视图,之前存储的值将恢复为 0.00(但在 table 中仍然正常!)。

为此苦苦挣扎了好几天,通过研究(odoo 网站和 Whosebug)找到了相关帖子,不幸的是,这些帖子对我都不起作用。

这是视图定义XML文件:

<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>

这是Python代码:

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}

通过调试我还发现,函数 get_default_threshold_price_mgr_sig 从未执行过...

我知道配置设置字段的工作方式与普通字段不同 - 只是不知道如何操作。

你知道我做错了什么吗?

应用程序设置的基本配置向导。它支持设置默认值、将组分配给员工用户以及安装模块。要制作这样的 'settings' 向导,请定义一个模型,如::

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(...),
        }

方法 execute 提供了一些基于命名约定的支持:

*   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.

方法 get_default 检索反映当前状态的值 'default_XXX'、'group_XXX' 和 'module_XXX' 等字段。它还调用所有方法 名称以 'get_default_' 开头;可以定义此类方法以提供 其他字段的当前值。

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.