我修改了现有字段date_due
。我依赖于multi_date_due
(我创建的one2many字段)',但在multi_date_due
被修改时它不会改变。但是,如果我在date_due
中将store设置为False,则该字段会正确更新。
我试图重新安装我的模块,以替换python和xml代码中的原始字段,但没办法。
Python代码:
class account_invoice(models.Model):
_inherit = 'account.invoice'
_logger = logging.getLogger(__name__)
date_due = fields.Date(
string='Due date',
readonly=False,
compute=lambda self: self._compute_date_due(),
store=True,
)
@api.multi
@api.depends('multi_date_due.date_due')
def _compute_date_due(self):
for record in self:
last_date = \
datetime.strptime("9999-12-31 23:59:59", "%Y-%m-%d %H:%M:%S")
if record.multi_date_due:
for multi_date_due in record.multi_date_due:
self._logger.warning(multi_date_due.date_due)
if datetime.strptime(\
multi_date_due.date_due, "%Y-%m-%d") < last_date:
first_date = multi_date_due.date_due
else:
first_date = None
record.date_due = first_date
XML代码:
<record id="invoice_supplier_form" model="ir.ui.view">
<field name="name">account.invoice.supplier.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_supplier_form"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='date_due']" position="replace">
<field name="date_due"/>
</xpath>
<xpath expr="//field[@name='date_due']" position="after">
<field name="multi_date_due" widget="one_to_many_list">
<tree string="Multiple due dates">
<field name="date_due" string="Due date"/>
<field name="invoice" invisible="1"/>
</tree>
</field>
</xpath>
</data>
</field>
</record>
此解决方案对我不起作用: Odoo 8 - Compute Field with "store=True" can't store in database
谢谢!
答案 0 :(得分:0)
字段定义中的lambda可能隐藏了装饰器。试试这个:
date_due = fields.Date(
string='Due date',
readonly=False,
compute='_compute_date_due',
store=True,
)
要强制重新计算所有记录,请删除列:
ALTER TABLE account_invoice DROP COLUMN date_due;
您的代码的最终建议:使用fields.Datetime.from_string()
和fields.Datetime.to_string()
而不是strptime
/ strftime
:它使代码更易于阅读。