【问题标题】:unrecognized field in odoo tree viewodoo树视图中无法识别的字段
【发布时间】:2018-01-13 00:22:59
【问题描述】:

我创建了一个与 Sell 有 One2many 关系的产品类:

class comp_product(models.Model):
    _name = "comp.product"
    _description = "Product Description"

    name = fields.Char('Product Name')
    description = fields.Text('add description of your product here')
    sell_ids = fields.One2many('comp.sell', 'product_id', String = "Sells")

和 Sells 类:

class comp_sell(models.Model):
    _name="comp.sell"
    _description="Sells per Product"

    name = fields.Float('How many units did you sell?')
    date = fields.datetime.today()
    product_id = fields.Many2one('comp.product', String = "Product", required = True)

在我看来,我添加了这段代码:

<notebook string="Other Informations">
    <page string="Description"><field name="description" string="Description"/></page>
    <page string="Update Sells">
        <field name="sell_ids">
            <tree string="Sells" editable="bottom">
                <field name="name"/>
                <field name="date" readonly="1" />
            </tree>
        </field>
    </page>
</notebook>

看起来 odoo 无法识别树内的字段。我收到了这个错误:

ParseError: "Error while validating constraint

Field `date` does not exist

有人知道问题出在哪里吗? 谢谢。

【问题讨论】:

    标签: python xml odoo-10 odoo


    【解决方案1】:

    问题在于您的字段date 定义,因为 fields.datetime.todaya function returning a string 而不是 Odoo 字段类型, 因此 date 被 Odoo 忽略。你应该写date = fields.Datetime(default=fields.Date.context_today)

    class comp_sell(models.Model):
        _name = 'comp.sell'
        _description = 'Sells per Product'
    
        name = fields.Float('How many units did you sell?')
        date = fields.Datetime(default=fields.Date.context_today)
        product_id = fields.Many2one('comp.product', string='Product', required=True)
    

    还要注意product_id 字段定义中的string 参数应为小写。

    【讨论】:

    • 有效!!我得到了我的树视图,但时间和日期没有更新,我添加的所有记录都有相同的时间:01:00:00。
    • 是的,不幸的是该字段在添加之前不存在(它被错误地声明并且未加载),因此 Odoo 为之前存在于表中的任何记录写入了相同的默认值(今天)。如果您还想在默认值date 中包含当前时间,则可以使用fields.Datetime.now
    • 是的。这段代码对我有用:date = fields.Datetime(default=lambda self: fields.datetime.now())
    猜你喜欢
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2020-11-03
    • 2017-02-03
    • 2020-12-19
    相关资源
    最近更新 更多