自动创建one2many字段

时间:2018-01-30 10:59:04

标签: python odoo-10

我希望我能够清楚地解释我的问题。在帐户模块中,我们有account.payment.termaccount.payment.term.line模型与one2many关系相关:

class AccountPaymentTerm(models.Model):
    _name = "account.payment.term"
    _description = "Payment Term"

    line_ids = fields.One2many('account.payment.term.line', 'payment_id', string='Terms', copy=True, default=_default_line_ids)
    period = fields.Selection([('month', '1 Month'),], string='Period', required=True, default='month', help="Select here the period between payments")
    how_much = fields.Float()
    fixed_amount = fields.Float()

class AccountPaymentTermLine(models.Model):
    _name = "account.payment.term.line"
    _description = "Payment Term Line"

    payment_id = fields.Many2one('account.payment.term', string='Payment Terms', required=True, index=True, ondelete='cascade')

我想在account.payment.term中创建一个自动创建付款条件行的方法。此方法应确定切片number_of_slices = (self.how_much/self.fixed_amount)的数量,这将是付款项行数。我现在尝试了这个代码:

@api.onchange('fixed_amount')
def automate_creation(self):
    terms = self.line_ids.browse([])
    self.number_of_slices = (self.how_much/self.fixed_amount)
    i = 0
    while i<10:
        terms+=terms.new({'value': 'fixed',
                          'value_amount':100,
                          'days':30,
                          'option':'day_after_invoice_date',
                          'payment_id':self._origin.id})
        i=i+1

此方法似乎不起作用。我没有在account.payment.term.line中找到我的专栏。

2 个答案:

答案 0 :(得分:1)

试试这个

@api.onchange('fixed_amount')
def automate_creation(self):
    self.number_of_slices = (self.how_much/self.fixed_amount)
    i = 0
    while i<10:
        term = self.line_ids.create({'value': 'fixed',
                                     'value_amount':100,
                                     'days':30,
                                     'option':'day_after_invoice_date',
                                     'payment_id':self._origin.id})
        self.line_ids |= term
        i=i+1

答案 1 :(得分:0)

此代码解决了我的问题,我使用function toggle_{{ capability }}() { var table, td, i; table = document.getElementById("myTable"); td = table.getElementsByClassName("div_{{ capability }}"); // Loop through all table rows, and hide those who don't match the search query if (td.parent().style.display === "none") { td.parent().style.display = ""; } else { td.parent().style.display = "none"; } } } 代替new

create