我在以表单而非树中添加
one2many
记录时需要它。
当您单击“保存并新建”时,会保存字段并保留一些字段不被清除以添加新记录。
制作
one2many
模态记住字段
记住[analaytic_account_id,product_id,vendor_id]
class MkVoucher(models.Model):
_name = 'mk.voucher'
voucher_line_ids = fields.One2many('mk.voucher.line', 'voucher_id', string='Voucher Lines', copy=True)
class MkVoucherLine(models.Model):
_name = 'mk.voucher.line'
voucher_id = fields.Many2one('mk.voucher' , string='Voucher')
product_id = fields.Many2one('product.product', string='Product',required=True)
analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account',required=True)
partner_id = fields.Many2one('res.partner', string='Vendor', required=True,)
product_id = fields.Many2one('product.product', string='Product', required=True, default=lambda self: self._default_product_id())
@api.model
def _default_product_id(self):
latest_line = self.env['mk.voucher.line'].search([], order="id desc", limit=1)
return latest_line.product_id.id if latest_line else False