【问题标题】:context doesn't update value of type上下文不更新类型的值
【发布时间】:2018-05-25 11:27:30
【问题描述】:

我在表单视图中有这两个按钮:

<button name="%(action_view_task_make_situation)d" string="Create work situation" type="action" states="open" context="{'type': 'situation'}"/>
<button name="%(action_make_general_final_count)d" string="Create Final General Count" type="action" states="done" context="{'type': 'final_count'}"/> 

通过这些操作:

<record id="action_view_task_make_situation" model="ir.actions.act_window">
    <field name="name">Make Situation</field>
    <field name="res_model">task.make.situation</field>
    <field name="type">ir.actions.act_window</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="target">new</field>
    <field name="context">{'type':True}</field>
</record>
<record id="action_make_general_final_count" model="ir.actions.act_window">
    <field name="name">Make General Final Count</field>
    <field name="res_model">task.make.situation</field>
    <field name="type">ir.actions.act_window</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="target">new</field>
    <field name="context">{'type':False}</field>
</record>   

现在我有task.make.situation 模型:

class TaskMakeSituation(models.TransientModel):

    _name = "task.make.situation"

    type = fields.Char(compute = "compute_type", string="Type", readonly= True)

    @api.multi
    def compute_type(self):
        if self._context.get('type', True):
            return "situation"
        else:
            return "final_count"

但是当我单击其中一个按钮时,向导会出现一个空的 type 字段。

【问题讨论】:

    标签: odoo-8 odoo odoo-10 odoo-9 odoo-11


    【解决方案1】:

    计算方法必须将它们的值直接“写入”到记录中:

    @api.multi
    def compute_type(self):
        context_type = self._context.get('type', True)
        for record in self:
            if context_type:
                record.type = "situation"
            else:
                record.type = "final_count"
    

    除此之外,您的解决方案应该只使用字段type 的默认值。首先将您的字段更改为普通字符字段:

    type = fields.Char(string="Type", readonly=True)
    

    小提示:本例中不需要参数string,因为较新的Odoo版本将使用字段名称来生成标签(字符串成为字段标签)例如:名称将获得名称或合作伙伴ID将获得合作伙伴(_id 将被删除)。

    现在更改按钮上下文,使用default_type

    <button name="%(action_view_task_make_situation)d" 
        string="Create work situation" type="action" states="open" 
        context="{'default_type': 'situation'}"/>
    <button name="%(action_make_general_final_count)d"
        string="Create Final General Count" type="action" states="done"
        context="{'default_type': 'final_count'}"/>
    

    前缀default_ 与字段名称组合,将在以后的默认提取中用于创建记录。

    【讨论】:

      【解决方案2】:

      只需尝试在上下文中键入之前添加“default_”,如下所示: context="{'default_type': False}" 并且只使用布尔值,因为您在 compute_type 中验证布尔值,也可以在视图中使用。

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 2022-11-13
        相关资源
        最近更新 更多