以下是我目前可用的选项:

我想增加一个叫‘Solved’的选项,用blue种 colored颜色 的圆圈.

我确实继承了"project t.ask."模型,添加了selection_add个方法,并用‘solved’文本覆盖了kanban_state个 Select 字段:

# Python File
from odoo import models, fields, api, _

class ProjectTask(models.Model):
    _inherit = 'project.task'

    kanban_state = fields.Selection(selection_add=[('solved', 'Solved')],
                                    ondelete={'solved': 'cascade'})

但是,它用红色圆圈显示为‘BLOCKED’值:

我肯定我错过了什么.

推荐答案

对于所有新状态, Select 状态都设计为使用red color.

要使用其他 colored颜色 ,您需要覆盖 Select 状态微件或创建自定义 Select 微件

100

1.创建新的 Select 状态小工具以在已解决状态下设置自定义 colored颜色

/** @odoo-module **/

import basicFields from "web.basic_fields";
import fieldRegistry from 'web.field_registry';
import { qweb } from 'web.core';


var CustomStateSelectionWidget = basicFields.StateSelectionWidget.extend({
    _prepareDropdownValues: function () {
        var self = this;
        var _data = [];
        var current_stage_id = self.recordData.stage_id && self.recordData.stage_id[0];
        var stage_data = {
            id: current_stage_id,
            legend_normal: this.recordData.legend_normal || undefined,
            legend_blocked : this.recordData.legend_blocked || undefined,
            legend_done: this.recordData.legend_done || undefined,
            legend_solved: this.recordData.legend_solved || undefined,
        };
        _.map(this.field.selection || [], function (selection_item) {
            var value = {
                'name': selection_item[0],
                'tooltip': selection_item[1],
            };
            if (selection_item[0] === 'normal') {
                value.state_name = stage_data.legend_normal ? stage_data.legend_normal : selection_item[1];
            } else if (selection_item[0] === 'done') {
                value.state_class = 'o_status_green';
                value.state_name = stage_data.legend_done ? stage_data.legend_done : selection_item[1];
            } else if (selection_item[0] === 'solved') {
                value.state_class = 'o_status_blue';
                value.state_name = stage_data.legend_solved ? stage_data.legend_solved : selection_item[1];
            } else {
                value.state_class = 'o_status_red';
                value.state_name = stage_data.legend_blocked ? stage_data.legend_blocked : selection_item[1];
            }
            _data.push(value);
        });
        return _data;
    },

    _render: function () {
        var states = this._prepareDropdownValues();
        // Adapt "FormSelection"
        // Like priority, default on the first possible value if no value is given.
        var currentState = _.findWhere(states, {name: this.value}) || states[0];
        this.$('.o_status')
            .removeClass('o_status_red o_status_green o_status_blue')
            .addClass(currentState.state_class)
            .prop('special_click', true)
            .parent().attr('title', currentState.state_name)
            .attr('aria-label', this.string + ": " + currentState.state_name);

        // Render "FormSelection.Items" and move it into "FormSelection"
        var $items = $(qweb.render('FormSelection.items', {
            states: _.without(states, currentState)
        }));
        var $dropdown = this.$('.dropdown-menu');
        $dropdown.children().remove(); // remove old items
        $items.appendTo($dropdown);

        // Disable edition if the field is readonly
        var isReadonly = this.record.evalModifiers(this.attrs.modifiers).readonly;
        this.$('a[data-toggle=dropdown]').toggleClass('disabled', isReadonly || false);
    },

});

fieldRegistry.add('custom_state_selection', CustomStateSelectionWidget);

return CustomStateSelectionWidget;

2.更新模型以使用已解决状态的图例字段

from odoo import models, fields, api, _

from odoo.addons import project
project.models.project.PROJECT_TASK_READABLE_FIELDS.add('legend_solved')


class ProjectTaskType(models.Model):
    _inherit = 'project.task.type'

    legend_solved = fields.Char(
        'Blue Kanban Label', default=lambda s: _('Solved'), translate=True, required=True,
        help='Override the default value displayed for the solved state for kanban selection when the task or issue is in that stage.')


class ProjectTask(models.Model):
    _inherit = 'project.task'

    kanban_state = fields.Selection(selection_add=[('solved', 'Solved')],
                                    ondelete={'solved': 'cascade'})

    legend_solved = fields.Char(related='stage_id.legend_solved', string='Kanban Valid Explanation', readonly=True,
                                related_sudo=False)

    @api.depends('stage_id', 'kanban_state')
    def _compute_kanban_state_label(self):
        for task in self:
            if task.kanban_state == 'normal':
                task.kanban_state_label = task.legend_normal
            elif task.kanban_state == 'blocked':
                task.kanban_state_label = task.legend_blocked
            elif task.kanban_state == 'solved':
                task.kanban_state_label = task.legend_solved
            else:
                task.kanban_state_label = task.legend_done

3.更改项目看板视图以设置 Select 状态小部件

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <record id="view_task_kanban_custom_state_selection" model="ir.ui.view">
        <field name="name">project.task.kanban.custom.state.selection</field>
        <field name="model">project.task</field>
        <field name="inherit_id" ref="project.view_task_kanban"/>
        <field name="arch" type="xml">
            <field name="kanban_state" position="attributes">
                <attribute name="widget">custom_state_selection</attribute>
            </field>
        </field>
    </record>
</odoo>

4.创建一个scss文件以声明自定义小部件中使用的o_status_blue

.o_status {
    &.o_status_blue {
        @extend .bg-primary;
    }
}

要加载js和scss文件,请在assets/web.assets_backend下添加文件路径,并在data下添加视图

'data': [
    'views/project_view.xml',
],
'assets': {
    'web.assets_backend': [
        'MODULE_NAME/static/src/js/StateSelectionWidget.js',
        'MODULE_NAME/static/src/css/StateSelectionWidget.scss',
    ],
},

Python相关问答推荐

云上Gunicorn的Flask-socketIO无法工作

根据多列和一些条件创建新列

如何观察cv2.erode()的中间过程?

Numpy索引argsorted使用integer数组,同时保留排序顺序

了解shuffle在NP.random.Generator.choice()中的作用

Polars Dataframe:如何按组删除交替行?

如何在Python中使用ijson解析SON期间检索文件位置?

Pydantic:如何将对象列表表示为dict(将列表序列化为dict)

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

如何自动抓取以下CSV

从webhook中的短代码(而不是电话号码)接收Twilio消息

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

韦尔福德方差与Numpy方差不同

运行总计基于多列pandas的分组和总和

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

将9个3x3矩阵按特定顺序排列成9x9矩阵

Python列表不会在条件while循环中正确随机化'

使用Python和文件进行模糊输出

网格基于1.Y轴与2.x轴显示在matplotlib中

关于两个表达式的区别