选择字段中添加了一个新状态。
state = fields.Selection(
selection_add=[
('draft_ok', 'Validated Quotation'),
],
)
现在我想在XML视图中显示它,其中当前显示以这种方式显示:
state
如果我从该视图中继承,以添加新状态:
<field name="state" widget="statusbar" statusbar_visible="draft,sent,sale"/>
在状态栏的末尾显示了新状态。我想在<xpath expr="//header/field[@name='state']" position="attributes">
<attribute name="statusbar_visible">draft,draft_ok,sent,sale</attribute>
</xpath>
和draft
我知道这样做的唯一方法是重新定义python的状态:
sent
但该解决方案不是很一致,因为如果其他模块还将状态添加到该字段,并且我的模块代码在其之后执行,我将销毁该其他模块添加的状态。
因此,我正在寻找其他方式以自定义订单显示现状栏。有什么想法吗?
以这种方式:
state = fields.Selection(
selection=[
('draft', 'Quotation'),
('draft_ok', 'Validated Quotation'),
('sent', 'Quotation Sent'),
('sale', 'Sales Order'),
('done', 'Locked'),
('cancel', 'Cancelled'),
],
)
从该女巫的代码中,位于
fields.Selection
例如,像monkey修补了
我尝试过正常
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()
它无效,我认为它需要很多工作。
这是我尝试的,它在odoo9中效果很好。
inheritance
selection_add_after
确保在定义字段之前进行修补
dictionary
您可以添加新键,例如删除或您想要的任何东西。
但猴子修补框架方法也是一个坏主意,因为如果 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
总是
this.
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