向 Odoo 11.0 帐户发票添加一个额外字段

Add an extra field to Odoo 11.0 Account Invoice

我想在继承自 account.invoice 的销售发票中添加一个额外的字段。更具体地说,我想向每个发票行项目添加一个字段 delivery_date

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
from datetime import date


class ReadyMixSalesInvoice(models.Model):
    _inherit = 'account.invoice'
    _name = 'account.invoice'

    delivery_date = fields.Datetime(string='Delivery Date', required=True, readonly=True, index=True,
                                    states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                                    help='Item delivery date.')

    @api.constrains('delivery_date')
    def _delivery_date_check(self):
        for record in self:
            if record.delivery_date and record.delivery_date.split(' ', 1)[0] < str(date.today()):
                raise ValidationError(_("Delivery Date must be after current date."))

和XML是:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="invoice_form_inherit_sale_ready_mix" model="ir.ui.view">
        <field name="name">account.invoice.form.readymix</field>
        <field name="model">account.invoice</field>
        <field name="inherit_id" ref="account.invoice_form"/>
        <field name="arch" type="xml">
            <data>
                <xpath expr="//field[@name='price_unit']" position="after">
                    <field name="delivery_date"/>
                </xpath>
                <xpath expr="//tree/field[@name='price_unit']" position="after">
                    <field name="delivery_date"/>
                </xpath>
            </data>
        </field>
    </record>

</odoo>

如果要向发票行添加新字段,则需要继承 account.invoice.line。 这里继承了account.invoice,继承了account.invoice.line再试了

例如:

class ReadyMixSalesInvoice(models.Model):
_inherit = 'account.invoice.line'
_name = 'account.invoice.line'

delivery_date = fields.Datetime(string='Delivery Date', required=True, readonly=True, index=True,
                                states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                                help='Item delivery date.')

@api.constrains('delivery_date')
def _delivery_date_check(self):
    for record in self:
        if record.delivery_date and record.delivery_date.split(' ', 1)[0] < str(date.today()):
            raise ValidationError(_("Delivery Date must be after current date."))

如果要将其添加到发票行,型号为 account.invoice.line
在这种情况下,您不需要指定 _name 属性,因为它是 已经继承自父模型..

在您的 python 代码中:

class ReadyMixSalesInvoice(models.Model):
    _inherit = 'account.invoice.line'