1

我正在尝试为我的自定义模型“product_images.product_image”实施 Odoo 发布工作流程。

我的模型如下所示:

# product_images/models/models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools

class PublishingStatus(models.Model):
    _name = 'product_images.publishing_status'
    _description = 'Publishing status'

    name = fields.Char(string="Name")
    slug = fields.Char(string="Slug")


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

    name = fields.Char(string="Alternative text")
    product_id = fields.Many2one('product.product', string='Product', ondelete='set null', index=True)
    original_image = fields.Binary(string='Original image')

    @api.model
    def _get_default_state(self):
        return self.env['product_images.publishing_status'].search([['slug', '=', 'draft']])

    @api.model
    def _get_all_states(self, groups, domain, order):
        state_ids = self.env['product_images.publishing_status'].search([])
        return state_ids

    state_id = fields.Many2one(
        'product_images.publishing_status',
        string='Publishing status',
        default=_get_default_state,
        group_expand='_get_all_states',
    )

    @api.multi
    def action_set_to_draft(self):
        self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'draft']])

    @api.multi
    def action_request_for_approval(self):
        self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'pending']])

    @api.multi
    def action_approve(self):
        self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'approved']])

    @api.multi
    def action_reject(self):
        self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'rejected']])

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

<!-- 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. 显示状态栏,但未激活当前发布状态。
  2. 如果我取消注释按钮,它们会引发有关无效域的错误:

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

我错过了什么?

4

1 回答 1

2

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

例如:

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 服务器并升级您的自定义模块。

于 2016-11-25T07:11:24.223 回答