Odoo中工作流模型的状态

时间:2016-11-24 21:37:52

标签: odoo-10

我正在尝试为自定义模型'product_images.product_image'实现Odoo发布工作流。

我的模特看起来像这样:

<!-- product_images/data/data.xml -->
<odoo>
    <data>
        <!-- explicit list view definition -->
        <record model="product_images.publishing_status" id="product_images.publishing_status_draft">
            <field name="name">Draft</field>
            <field name="slug">draft</field>
        </record>
        <record model="product_images.publishing_status" id="product_images.publishing_status_pending">
            <field name="name">Pending</field>
            <field name="slug">pending</field>
        </record>
        <record model="product_images.publishing_status" id="product_images.publishing_status_approved">
            <field name="name">Approved</field>
            <field name="slug">approved</field>
        </record>
        <record model="product_images.publishing_status" id="product_images.publishing_status_rejected">
            <field name="name">Rejected</field>
            <field name="slug">rejected</field>
        </record>
    </data>
</odoo>

然后我有一些发布状态的数据记录:

<odoo>
    <data>
        <record model="workflow" id="product_images.wkf_image_publishing">
            <field name="name">Product Image Publishing Workflow</field>
            <field name="osv">product_images.product_image</field>
            <field name="on_create">True</field>
        </record>

        <record model="workflow.activity" id="product_images.wkf_activity_draft">
            <field name="name">Draft</field>
            <field name="wkf_id" ref="product_images.wkf_image_publishing" />
            <field name="flow_start" eval="True" />
            <field name="kind">function</field>
            <field name="action">action_set_to_draft()</field>
        </record>
        <record model="workflow.activity" id="product_images.wkf_activity_pending">
            <field name="name">Pending</field>
            <field name="wkf_id" ref="product_images.wkf_image_publishing" />
            <field name="kind">function</field>
            <field name="action">action_request_for_approval()</field>
        </record>
        <record model="workflow.activity" id="product_images.wkf_activity_approved">
            <field name="name">Approved</field>
            <field name="wkf_id" ref="product_images.wkf_image_publishing" />
            <field name="flow_stop" eval="True" />
            <field name="kind">function</field>
            <field name="action">action_approve()</field>
        </record>
        <record model="workflow.activity" id="product_images.wkf_activity_rejected">
            <field name="name">Rejected</field>
            <field name="wkf_id" ref="product_images.wkf_image_publishing" />
            <field name="flow_stop" eval="True" />
            <field name="kind">function</field>
            <field name="action">action_reject()</field>
        </record>

        <record model="workflow.transition" id="product_images.wkf_transition_draft_to_pending">
            <field name="act_from" ref="product_images.wkf_activity_draft" />
            <field name="act_to" ref="product_images.wkf_activity_pending" />
            <field name="condition">name != "" and original_image != ""</field>
            <field name="signal">pending</field>
        </record>
        <record model="workflow.transition" id="product_images.wkf_transition_pending_to_draft">
            <field name="act_from" ref="product_images.wkf_activity_pending" />
            <field name="act_to" ref="product_images.wkf_activity_draft" />
            <field name="signal">draft</field>
        </record>
        <record model="workflow.transition" id="product_images.wkf_transition_pending_to_approved">
            <field name="act_from" ref="product_images.wkf_activity_pending" />
            <field name="act_to" ref="product_images.wkf_activity_approved" />
            <field name="signal">approve</field>
        </record>
        <record model="workflow.transition" id="product_images.wkf_transition_pending_to_rejected">
            <field name="act_from" ref="product_images.wkf_activity_pending" />
            <field name="act_to" ref="product_images.wkf_activity_rejected" />
            <field name="signal">reject</field>
        </record>
    </data>
</odoo>

我还有一些记录可以创建一个允许在发布状态之间切换的工作流程:

    <record model="ir.ui.view" id="product_images.form">
        <field name="name">Product Image</field>
        <field name="model">product_images.product_image</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <!--
                    <button name="draft"
                            type="workflow"
                            string="Set to draft"
                            attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
                    />
                    <button name="pending"
                            type="workflow"
                            string="Request for approval"
                            attrs="{'invisible': [('state_id.slug','not in',['draft'])]}"
                    />
                    <button name="approve" 
                            type="workflow"
                            string="Approve"
                            attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
                            class="oe_highlight"
                    />
                    <button name="reject" 
                            type="workflow"
                            string="Reject"
                            attrs="{'invisible': [('state_id.slug','not in',['pending'])]}"
                            class="oe_highlight"
                    />
                    -->
                    <field name="state_id" widget="statusbar" />
                </header>

                <sheet>
                    <group>
                        <field name="product_id" />
                        <field name="name" string="Alternative text" />
                        <field name="original_image" widget="image" class="oe_avatar" />
                        <field name="state_id" />
                    </group>
                </sheet>
            </form>
        </field>
    </record>

现在是最奇怪的部分!我需要一个带有按钮的表单,用于在工作流状态和状态栏之间切换,显示当前活动状态。这就是我试过的:

{{1}}

我遇到的问题:

  1. 显示状态栏,但未激活当前发布状态。
  2. 如果我取消注释按钮,则会抛出有关无效域的错误:
  3.   

    未捕获错误:域中的未知字段state_id.slug   [[“state_id.slug”,“not in”,[“pending”]]]

    我错过了什么?

1 个答案:

答案 0 :(得分:2)

domain 属性中,我们不能在左侧使用父字段。在您的情况下,我们需要添加相关字段。

例如:

class ProductImage(models.Model):
    _name = 'product_images.product_image'

    slug = fields.Char(related='state_id.slug', string='Slug', store=True)

在视图文件中的 state_id 之后放置 slug 字段

<field name="slug" invisible="1"/>

现在取消注释<button>代码。

然后,重新启动Odoo服务器并升级自定义模块。