Из кода, ответственного за эту ведьму, расположенную в 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()
Как, например, исправление обезьян Я попытался нормально inheritance
Не сработало, я думаю, что нужно много работы.
Это то, что я пробовал, и оно прекрасно работало в Odoo 9. Я создал новый ключ selection_add_after
witch is 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
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')]})
Вы можете добавить новый ключ, например, удалить или что угодно.
Но метод Monkey Patching Framework также является плохой идеей, потому что если есть обновления в _setup_attrs
всегда
удалено этим.
EDIT
Для 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