python - 如何将状态添加到选择字段并在 Odoo 11 的状态栏中显示它的顺序?

标签 python xml python-3.x odoo odoo-11

我使用参数 selection_addSelection 字段添加了一个新状态:

state = fields.Selection(
    selection_add=[
        ('draft_ok', 'Validated Quotation'),
    ],
)

现在我想在 XML View 中显示它,当前 state 是这样显示的:

<field name="state" widget="statusbar" statusbar_visible="draft,sent,sale"/>

如果我从该 View 继承,添加新状态:

<xpath expr="//header/field[@name='state']" position="attributes">
    <attribute name="statusbar_visible">draft,draft_ok,sent,sale</attribute>
</xpath>

新状态显示在状态栏的末尾。我想在 draftsent 状态之间显示它。

我知道的唯一方法是在 Python 中重新定义状态:

state = fields.Selection(
    selection=[
        ('draft', 'Quotation'),
        ('draft_ok', 'Validated Quotation'),
        ('sent', 'Quotation Sent'),
        ('sale', 'Sales Order'),
        ('done', 'Locked'),
        ('cancel', 'Cancelled'),
    ],
)

但是这个解决方案不是很一致,因为如果其他模块也向这个字段添加状态并且我的模块代码在它之后执行,我会破坏这个其他模块添加的状态。

所以我正在寻找其他方式来显示带有自定义顺序的状态栏。有什么想法吗?

最佳答案

从位于 fields.Selection class 中的这个女巫的代码负责 没有办法做到这一点 不使用特殊技巧:

# frame code
def _setup_attrs(self, model, name):
    super(Selection, self)._setup_attrs(model, name)
    # determine selection (applying 'selection_add' extensions)
    for field in reversed(resolve_mro(model, name, self._can_setup_from)):
        # We cannot use field.selection or field.selection_add here
        # because those attributes are overridden by ``_setup_attrs``.
        if 'selection' in field.args:
            self.selection = field.args['selection']
        if 'selection_add' in field.args:
            # use an OrderedDict to update existing values
            selection_add = field.args['selection_add']
            self.selection = OrderedDict(self.selection + selection_add).items()

例如monkey patching我尝试了正常的inheritance它没有用我认为它需要大量的工作。

这是我尝试过的,它在 Odoo 9 中工作得很好。 我创建了一个新 key 是 selection_add_after 女巫是 字典

    1. key is the value of selection that you want to add item after it

    2. value is the list of selection items that you want to add
    def _setup_attrs(self, model, name):
        super(fields.Selection, self)._setup_attrs(model, name)
        # determine selection (applying 'selection_add' extensions)
        for field in reversed(fields.resolve_mro(model, name, self._can_setup_from)):
            # We cannot use field.selection or field.selection_add here
            # because those attributes are overridden by ``_setup_attrs``.
            if 'selection' in field.args:
                self.selection = field.args['selection']
            if 'selection_add' in field.args:
                # use an OrderedDict to update existing values
                selection_add = field.args['selection_add']
                self.selection = OrderedDict(self.selection + selection_add).items()
            if 'selection_add_after' in field.args:
                selection_add_atfer = field.args['selection_add_after']
                new_selection = []
                for item in self.selection:
                    new_selection.append(item) # add the element firs
                    items_to_add = selection_add_atfer.get(item[0], [])
                    for item_to_add in items_to_add:  # then add the element if there is
                        new_selection.append(item_to_add)
                # I don't know why they used OrderdedDict ???!! do you have any idea why?!!
                self.selection = OrderedDict(new_selection).items()

    # mucky patch the method in selection field
    fields.Selection._setup_attrs = _setup_attrs

确保在定义字段之前打补丁

    # add element after draft 
    state = fields.Selection(selection_add_after={'draft': [('hello', 'Hello')]})

    # add element after draft and other emelent after confirmed 
    state = fields.Selection(selection_add_after={'draft': [('hello', 'Hello')], 'confirmed': [('test','Test')]})

您可以添加新 key ,如删除或任何您想要的。

但是猴子修补框架方法也是一个坏主意,因为如果 _setup_attrs 中有任何更新 总是 已删除

编辑

对于 Odoo 11,这是代码:

def _setup_attrs(self, model, name):
    super(fields.Selection, self)._setup_attrs(model, name)
    # determine selection (applying 'selection_add' extensions)
    for field in reversed(fields.resolve_mro(model, name, self._can_setup_from)):
        # We cannot use field.selection or field.selection_add here
        # because those attributes are overridden by ``_setup_attrs``.
        if 'selection' in field.args:
            self.selection = field.args['selection']
        if 'selection_add' in field.args:
            # use an OrderedDict to update existing values
            selection_add = field.args['selection_add']
            self.selection = list(OrderedDict(self.selection + selection_add).items())
        if 'selection_add_after' in field.args:
            selection_add_atfer = field.args['selection_add_after']
            new_selection = []
            for item in self.selection:
                new_selection.append(item) # add the element firs
                items_to_add = selection_add_atfer.get(item[0], [])
                for item_to_add in items_to_add:  # then add the element if there is
                    new_selection.append(item_to_add)
            # I don't know why they used OrderdedDict ???!! do you have any idea why?!!
            self.selection = list(OrderedDict(new_selection).items())

fields.Selection._setup_attrs = _setup_attrs

关于python - 如何将状态添加到选择字段并在 Odoo 11 的状态栏中显示它的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56252165/

相关文章:

xml - 我如何强制浏览器呈现文件 XHTML5,HTML5 的 XML 序列化?

python - 如何使用 numba 创建给定类型的 numpy 数组

python - 尝试使用 pip 安装 matplotlib 和 numpy 时出现错误

python - 获取数据帧的相同随机事件

.net - 调用 XmlReader::ReadStartElement 时抛出 InvalidOperationException

java - 解析 xml 文档的更优雅的方式

multithreading - 如何在读取流时正确终止 Python3 线程

python - 轮渡票务程序 Python

python - SQL 计数优化

python - 如何从 xgboost CV 中获取经过训练的模型?