0

如何根据条件更改字段?例子:

模型.py:

field_1 = field.float
field_2 = field.percentage

xml:

I don't know how to do this in XML but I will just show what I want.

form view
        name: [            ]
        commission:  [ field_1 or field ] <=  this will change the format depends on the type below.
        type: selector by default in "amount" upon checking "percentage"

tree view:
       <field name="name"/>
       <field commission="field1 or field2"> <= I don't know how to code this.
       <field type="type"/>

这是我的代码模型:

class architecture_page(models.Model):
    _inherit = 'sale.order'

    @api.constrains('architect_com_type', 'architect_commission')
    def _validate_commission(self):
        for field in self:
            if field.architect_com_type == 'Percentage':
                if (field.architect_com_type > 100) or (field.architect_com_type <= 0):
                    raise exceptions.ValidationError(
                        "Percentage fields must be less than equal to 100 or greater than 0")

这是我的 XML 代码:

<odoo>
    <record id="view_order_form" model="ir.ui.view">
        <field name="name">Architecture</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='order_lines']" position="after">
                <page string="Architect and Interior Designer">
                    <tree string="Architect and Interior Designer"
                          editable="bottom"
                    >
                        <control>
                            <create name="add_architect_control" string="Add a architect"/>
                        </control>

                        <field name="architect_id"/>
                        <field name="architect_commission"/>
                        <field name="architect_com_type"/>
                    </tree>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

我正在尝试创建一个可编辑的树视图,它类似于报价单中的订单行,我们可以在其中添加新单位或产品。

在此处输入图像描述

但我的不是这样。

在此处输入图像描述

遇到一些错误:

Error:
Odoo Server Error

Traceback (most recent call last):
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 392, in _check_xml
    self.postprocess_and_fields(view.model, view_doc, view.id)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 952, in postprocess_and_fields
    fields_def = self.postprocess(model, node, view_id, False, fields)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 882, in postprocess
    fields.update(self.postprocess(model, f, view_id, in_tree_view, model_fields))
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 882, in postprocess
    fields.update(self.postprocess(model, f, view_id, in_tree_view, model_fields))
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 882, in postprocess
    fields.update(self.postprocess(model, f, view_id, in_tree_view, model_fields))
  [Previous line repeated 1 more time]
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 803, in postprocess
    xarch, xfields = self.with_context(
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 964, in postprocess_and_fields
    self.raise_view_error(message, view_id)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 592, in raise_view_error
    raise ValueError(message)
ValueError: Field `architect_ids` does not exist

Error context:
View `architect.page.view`
[view_id: 846, xml_id: n/a, model: sale.order, parent_id: 756]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 712, in parse
    self._tag_root(de)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 674, in _tag_root
    f(rec)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 577, in _tag_record
    record = model._load_records([data], self.mode == 'update')
  File "D:\dev\odoo-13.0\odoo\models.py", line 4067, in _load_records
    records = self._load_records_create([data['values'] for data in to_create])
  File "D:\dev\odoo-13.0\odoo\models.py", line 3981, in _load_records_create
    return self.create(values)
  File "<decorator-gen-32>", line 2, in create
  File "D:\dev\odoo-13.0\odoo\api.py", line 336, in _model_create_multi
    return create(self, arg)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 473, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "D:\dev\odoo-13.0\odoo\api.py", line 336, in _model_create_multi
    return create(self, arg)
  File "D:\dev\odoo-13.0\odoo\models.py", line 3762, in create
    fields[0].determine_inverse(batch_recs)
  File "D:\dev\odoo-13.0\odoo\fields.py", line 1114, in determine_inverse
    getattr(records, self.inverse)()
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 299, in _inverse_arch
    view.write(data)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 490, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "D:\dev\odoo-13.0\odoo\models.py", line 3551, in write
    real_recs._validate_fields(set(vals) - set(inverse_fields))
  File "D:\dev\odoo-13.0\odoo\models.py", line 1167, in _validate_fields
    check(self)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 394, in _check_xml
    raise ValidationError("%s\n\n%s" % (_("Error while validating view"), tools.ustr(e)))
odoo.exceptions.ValidationError: ('Error while validating view\n\nField `architect_ids` does not exist\n\nError context:\nView `architect.page.view`\n[view_id: 846, xml_id: n/a, model: sale.order, parent_id: 756]', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\dev\odoo-13.0\odoo\http.py", line 619, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "D:\dev\odoo-13.0\odoo\http.py", line 309, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "D:\dev\odoo-13.0\odoo\tools\pycompat.py", line 14, in reraise
    raise value
  File "D:\dev\odoo-13.0\odoo\http.py", line 664, in dispatch
    result = self._call_function(**self.params)
  File "D:\dev\odoo-13.0\odoo\http.py", line 345, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "D:\dev\odoo-13.0\odoo\service\model.py", line 93, in wrapper
    return f(dbname, *args, **kwargs)
  File "D:\dev\odoo-13.0\odoo\http.py", line 338, in checked_call
    result = self.endpoint(*a, **kw)
  File "D:\dev\odoo-13.0\odoo\http.py", line 910, in __call__
    return self.method(*args, **kw)
  File "D:\dev\odoo-13.0\odoo\http.py", line 510, in response_wrap
    response = f(*args, **kw)
  File "d:\dev\odoo-13.0\addons\web\controllers\main.py", line 1324, in call_button
    action = self._call_kw(model, method, args, kwargs)
  File "d:\dev\odoo-13.0\addons\web\controllers\main.py", line 1312, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "D:\dev\odoo-13.0\odoo\api.py", line 387, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "D:\dev\odoo-13.0\odoo\api.py", line 374, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-65>", line 2, in button_immediate_upgrade
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_module.py", line 72, in check_and_log
    return method(self, *args, **kwargs)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_module.py", line 629, in button_immediate_upgrade
    return self._button_immediate_function(type(self).button_upgrade)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_module.py", line 573, in _button_immediate_function
    modules.registry.Registry.new(self._cr.dbname, update_module=True)
  File "D:\dev\odoo-13.0\odoo\modules\registry.py", line 86, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "D:\dev\odoo-13.0\odoo\modules\loading.py", line 417, in load_modules
    processed_modules += load_marked_modules(cr, graph,
  File "D:\dev\odoo-13.0\odoo\modules\loading.py", line 313, in load_marked_modules
    loaded, processed = load_module_graph(
  File "D:\dev\odoo-13.0\odoo\modules\loading.py", line 225, in load_module_graph
    load_data(cr, idref, mode, kind='data', package=package, report=report)
  File "D:\dev\odoo-13.0\odoo\modules\loading.py", line 68, in load_data
    tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 736, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 803, in convert_xml_import
    obj.parse(doc.getroot())
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 715, in parse
    pycompat.reraise(
  File "D:\dev\odoo-13.0\odoo\tools\pycompat.py", line 13, in reraise
    raise value.with_traceback(tb)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 712, in parse
    self._tag_root(de)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 674, in _tag_root
    f(rec)
  File "D:\dev\odoo-13.0\odoo\tools\convert.py", line 577, in _tag_record
    record = model._load_records([data], self.mode == 'update')
  File "D:\dev\odoo-13.0\odoo\models.py", line 4067, in _load_records
    records = self._load_records_create([data['values'] for data in to_create])
  File "D:\dev\odoo-13.0\odoo\models.py", line 3981, in _load_records_create
    return self.create(values)
  File "<decorator-gen-32>", line 2, in create
  File "D:\dev\odoo-13.0\odoo\api.py", line 336, in _model_create_multi
    return create(self, arg)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 473, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "D:\dev\odoo-13.0\odoo\api.py", line 336, in _model_create_multi
    return create(self, arg)
  File "D:\dev\odoo-13.0\odoo\models.py", line 3762, in create
    fields[0].determine_inverse(batch_recs)
  File "D:\dev\odoo-13.0\odoo\fields.py", line 1114, in determine_inverse
    getattr(records, self.inverse)()
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 299, in _inverse_arch
    view.write(data)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 490, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "D:\dev\odoo-13.0\odoo\models.py", line 3551, in write
    real_recs._validate_fields(set(vals) - set(inverse_fields))
  File "D:\dev\odoo-13.0\odoo\models.py", line 1167, in _validate_fields
    check(self)
  File "D:\dev\odoo-13.0\odoo\addons\base\models\ir_ui_view.py", line 394, in _check_xml
    raise ValidationError("%s\n\n%s" % (_("Error while validating view"), tools.ustr(e)))
odoo.tools.convert.ParseError: "Error while validating view

Field `architect_ids` does not exist

Error context:
View `architect.page.view`
[view_id: 846, xml_id: n/a, model: sale.order, parent_id: 756]
None" while parsing file:/d:/dev/odoo-13.0/custom/architect_page/views/architect_page.xml:1, near
<odoo>
    <record id="view_order_form" model="ir.ui.view">
        <field name="name">architect.page.view</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='order_lines']" position="after">
                <page string="Architect and Interior Designer">
                    <field name="architect_ids">
                        <tree string="Architect and Interior Designer" editable="bottom">
                            <control>
                                <create name="add_architect_control" string="Add a architect"/>
                            </control>
                            <field name="architect_ids"/>
                            <field name="architect_commission"/>
                            <field name="architect_com_type"/>
                        </tree>
                    </field>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

模型:

class ArchitectPage(models.Model):
    _inherit = 'sale.order'

    architect_ids = fields.One2many(string='Architect',
                                    comodel_name='res.architect', inverse_name='sale_id')

res_architect:

class ResArchitect(models.Model):
    _name = 'res.architect'
    name = fields.Char('name')
    architect_com_type = fields.Selection(string='Commission type',
                                          selection=[('percentage', 'Percentage'), ('float', 'Float')])
    architect_commission = fields.Float(string='Architect Commission')
    sale_id = fields.Many2one(string='sale', comodel_name='sale.order')

    # put your constraint here
    @api.constrains('architect_com_type', 'architect_commission')
    def _validate_commission(self):
        for field in self:
            if field.architect_com_type == 'Percentage':
                if (field.architect_com_type > 100) or (field.architect_com_type <= 0):
                    raise exceptions.ValidationError(
                        "Percentage fields must be less than equal to 100 or greater than 0")
4

1 回答 1

1

您有一个表单视图view_order_form,您需要 one2many/many2many 字段,要在其中创建树视图,您需要创建一个新模块“res.architect”或其他东西,并使用 one2many 将其链接到 sale.order 模块,然后您可以在您的选项卡中创建建筑师的树形视图。

res_architect.py

from odoo import models, fields

class ResArchitect(models.Model):
    _name = 'res.architect'
    name = fields.Chat('name')
    architect_com_type = fields.Selection(string='Commision type',selection=[('percentage','Percentage'),('float','Float')])
    architect_commission = fields.Float(string='Architect Commission')
    sale_id = fields.Many2one(string='sale', comodel_name='sale.order')

    # put your contraint here
    @api.constrains('architect_com_type', 'architect_commission')
    def _validate_commission(self):
        for field in self:
            if field.architect_com_type == 'Percentage':
                if (field.architect_com_type > 100) or (field.architect_com_type <= 0):
                    raise exceptions.ValidationError(
                        "Percentage fields must be less than equal to 100 or greater than 0")

然后在您的销售订单模型中

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    architect_ids = fields.One2many(string='Architect', 
    comodel_name='res.architect', inverse_name='sale_id')

然后像这样更新您的视图:

<odoo>
    <record id="view_order_form" model="ir.ui.view">
        <field name="name">Architecture</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='order_lines']" position="after">
                <page string="Architect and Interior Designer">
                    <field name='architect_ids'>                    
                        <tree string="Architect and Interior Designer"
                              editable="bottom">
                        <control>
                            <create name="add_architect_control" string="Add a architect"/>
                        </control>

                        <field name="architect_commission"/>
                        <field name="architect_com_type"/>
                    </tree>
                        </field>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

我希望这对您有所帮助,如果您需要任何进一步的帮助,请随时联系我!

于 2020-03-20T16:12:49.797 回答