如何在 odoo 10 中覆盖 TransientModel 的 fields_view_get?

How to override fields_view_get of TransientModel in odoo 10?

我已经做到了,在旧的 odoo 版本中,这种方法有效! 在日志文件中看不到这个 'kecske' 信号。没有错误信息。如果我在super之前写了一些代码,它没有任何效果。

有什么想法吗?方法对吗?

class DemoWizard(models.TransientModel):
    _name = 'demo.wizard'

    name = fields.Char(string='Name')

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        log = logging.getLogger('demo.wizard.fields_view_get()')
        log.debug('kecske')
        return super(DemoWizard,self).fields_view_get(view_id, view_type, toolbar, submenu)

这是Odoo10源码。该文件位于匿名插件中。 odoo/addons/anonymization/wizard/anonymize_wizard.py。请注意对 super() 的调用以及与位置参数相对应的关键字参数的使用。

除此之外,您的代码看起来是正确的。

在您的示例中,您使用不同的技术初始化了日志记录。尝试按如下方式初始化您的记录器。

log = logging.getLogger(__name__)
log.info("My Log Message")

或用于调试。

log.debug("My debug message")

info,debug,warning,error可用于记录不同严重程度的日志消息。

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    state = self.env['ir.model.fields.anonymization']._get_global_state()
    step = self.env.context.get('step', 'new_window')
    res = super(IrModelFieldsAnonymizeWizard, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
    eview = etree.fromstring(res['arch'])
    placeholder = eview.xpath("group[@name='placeholder1']")
    if len(placeholder):
        placeholder = placeholder[0]
        if step == 'new_window' and state == 'clear':
            # clicked in the menu and the fields are not anonymized: warn the admin that backuping the db is very important
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Warning'}))
            eview.remove(placeholder)
        elif step == 'new_window' and state == 'anonymized':
            # clicked in the menu and the fields are already anonymized
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('field', {'name': 'file_import', 'required': "1"}))
            placeholder.addnext(etree.Element('label', {'string': 'Anonymization file'}))
            eview.remove(placeholder)
        elif step == 'just_anonymized':
            # we just ran the anonymization process, we need the file export field
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('field', {'name': 'file_export'}))
            # we need to remove the button:
            buttons = eview.xpath("button")
            for button in buttons:
                eview.remove(button)
            # and add a message:
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Result'}))
            # remove the placeholer:
            eview.remove(placeholder)
        elif step == 'just_desanonymized':
            # we just reversed the anonymization process, we don't need any field
            # we need to remove the button
            buttons = eview.xpath("button")
            for button in buttons:
                eview.remove(button)
            # and add a message
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Result'}))
            # remove the placeholer:
            eview.remove(placeholder)
        else:
            raise UserError(_("The database anonymization is currently in an unstable state. Some fields are anonymized,"
                              " while some fields are not anonymized. You should try to solve this problem before trying to do anything else."))
        res['arch'] = etree.tostring(eview)
    return res