【发布时间】:2019-05-09 13:52:01
【问题描述】:
以下 Python 脚本不会创建日期字段:
from datetime import datetime
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class DealInvoice(models.Model):
_inherit = 'account.invoice'
x_date_deal = fields.Date(string='Date Deal',
required=True,
readonly=True,
default=(date.today()),
index=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
help='Item date deal.')
@api.constrains('x_date_deal')
def _x_date_deal_check(self):
for record in self:
if record.x_date_deal and record.x_date_deal [0] <= str(date.today()):
raise ValidationError(_("Date deal must be before current date."))
【问题讨论】:
-
如果该字段尚未创建,则表示该字段
x_date_deal在数据库中的表account_invoice上不存在,请检查您的__init__.py文件以确保您的文件正在导入,然后重启 Odoo 并升级模块。 -
如果您的意思是在视图中看不到新创建的字段,那是因为您需要使用继承对其进行修改。
-
通过不同的方式解决了:
-
from odoo import api, fields, models class AccountInvoice(models.Model): _inherit = 'account.invoice' x_date_deal = fields.Date(string="Date of Deal")
-
我刚刚看到了
readonly=True选项。使用它会使 Odoo 忽略用户放在那里的任何内容。它应与_compute一起使用