Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Odoo 10中的onchange方法中向One2many字段添加一些新记录?_Python_Python 2.7_Odoo_Odoo 10 - Fatal编程技术网

Python 如何在Odoo 10中的onchange方法中向One2many字段添加一些新记录?

Python 如何在Odoo 10中的onchange方法中向One2many字段添加一些新记录?,python,python-2.7,odoo,odoo-10,Python,Python 2.7,Odoo,Odoo 10,我试图通过onchange方法修改One2many字段。我需要的是添加一些记录,但在onchange计算之前保留一些现有记录 更准确地说,我有一个名为tax\u line\u id的字段。该字段存储税款。税收有一个名为manual的字段,一个Boolean 因此,每次字段my_字段更改时,我都需要向其中添加新的税2任何字段tax_行_id,但我需要保留已将manual设置为True的旧字段 尝试一次 @api.onchange('my_field') def onchange_my_field(

我试图通过onchange方法修改One2many字段。我需要的是添加一些记录,但在onchange计算之前保留一些现有记录

更准确地说,我有一个名为
tax\u line\u id
的字段。该字段存储税款。税收有一个名为
manual
的字段,一个
Boolean

因此,每次字段
my_字段
更改时,我都需要向其中添加新的税2任何字段
tax_行_id
,但我需要保留已将
manual
设置为True的旧字段

尝试一次

@api.onchange('my_field')
def onchange_my_field(self):
    new_tax_lines = []
    tax_line_ids = self.tax_line_ids.filtered(
        lambda r: r.manual).ids
    # get_taxes_values returns a list of dictionaries with values
    tax_grouped = self.get_taxes_values()
    for tax in tax_grouped.values():
        new_tax = self.env['account.invoice.tax'].create(tax)
        tax_line_ids.append(new_tax.id)
    self.tax_line_ids = [(6, 0, tax_line_ids)]
问题

onchange方法工作正常,但当我单击Save按钮时,onchange方法引入的
tax\u-line\u-id
记录消失(
tax\u-line\u-id
不是只读字段)

尝试B

@api.onchange('my_field')
def onchange_my_field(self):
    new_tax_lines = []
    manual_tax_lines = self.tax_line_ids.filtered(
        lambda r: r.manual)
    for manual_tax_line in manual_tax_lines:
        new_tax_lines.append(manual_tax_line.sudo().read()[0])
    tax_grouped = self.get_taxes_values()
    for tax in tax_grouped.values():
        new_tax_lines.append((0, 0, tax))
    self.tax_line_ids = new_tax_lines
问题

onchange方法在第一次运行时效果良好,即使我单击了保存按钮(记录不会消失),但如果我在保存前第二次修改
my_字段
,我会收到一个安全访问错误(我正在与管理员一起工作):

(文档类型:account.invoice.tax,Operación:read)

这就是为什么我在
read()
之前添加了
sudo()
,以防万一,但错误仍然存在

结论

因此,在我的onchange方法中,如何向One2many字段添加新记录,以保留一些现有记录?

forvas

试着这样做, 返回{'value':{'your_one2many_字段:列出_id}

谢谢

试着这样做

@api.onchange('my_field')
def onchange_my_field(self):
    account_invoice_tax = self.env['account.invoice.tax']
    for invoice in self:
        invoice._cr.execute("DELETE FROM account_invoice_tax WHERE invoice_id=%s AND manual is False", (invoice.id,))
        if invoice._cr.rowcount:
            invoice.invalidate_cache()
        tax_grouped = invoice.get_taxes_values()

        # Create new tax lines
        for tax in tax_grouped.values():
            account_invoice_tax.create(tax)

我刚刚尝试了现有的方法。

尝试A不应该起作用,因为
create
在onchange环境中不起作用。我做了几天类似的事情,但我不确定最终是否有可行的解决方案。Odoo 10现在已经变老了:-/好的,解决方案A出来了,但是B。。。[(0,0,values)]系统在onchange中工作。[(4,ID)]系统也是如此。如何将两者结合起来?如果它们不能组合,为什么我会使用
read
方法得到一个安全错误?你知道吗?不知道,访问错误很奇怪,因为你已经在使用
sudo
。这个错误消息可能掩盖了另一个错误?好的,我发现了问题,但没有找到解决方案。
read()。因此,当我触发onchange时,我会保留一个手动税,用
read()
复制它的值,然后用
(0,0,VAL)
再次创建它,但是在这之后,如果我再次触发onchange,该手动税没有ID,因此我无法调用
read()
。我怎样才能得到它的值呢?我会调试一下,看看在这两种情况下奥多给了你什么。必须有一个解决方案,这将是丑陋的。你能更详细地描述一下为什么这应该有效吗?为什么要使用旧的API风格的编码,这对Odoo11及以上版本没有帮助。@CZoellner是的,当然,我会在正确阅读问题之前解释一下?我不这么认为。首先,这是对odoo-10版本的质疑,是的,它可以在该版本中用于将数据存储在one2many字段中。odoo 10版本没有一种非常有效的方法来清除缓存,有时您无法获得结果[这不会保存您的更改]。所以这可能是存档的方式。嗨@OpenCode,谢谢你的回答。第二种解决方案与我的第一次尝试相同。直到我点击Save按钮,记录消失(因为@CZoellner在问题中的评论),它似乎工作正常。第一个解决方案给出了一个语法错误,但这个想法很好,我尝试传递一个结合
(4,ID)
(0,0,值)
的列表。但结果是以下错误:
编程错误:无法适应类型“生成器”
。您好@Vishnu,谢谢您的回答。该代码在
onchange
方法中不起作用。它给出了错误
编程错误:无法调整类型“NewId”
@api.onchange('my_field')
def onchange_my_field(self):
    new_tax_lines = []
    tax_lines = self.tax_line_ids.filtered(
        lambda r: r.manual)
    # get_taxes_values returns a list of dictionaries with values
    tax_grouped = self.get_taxes_values()
    for tax in tax_grouped.values():
        new_tax = self.env['account.invoice.tax'].create(tax)
        tax_lines += new_tax
    self.tax_line_ids = [(6, 0, tax_lines.ids)]
@api.onchange('my_field')
def onchange_my_field(self):
    account_invoice_tax = self.env['account.invoice.tax']
    for invoice in self:
        invoice._cr.execute("DELETE FROM account_invoice_tax WHERE invoice_id=%s AND manual is False", (invoice.id,))
        if invoice._cr.rowcount:
            invoice.invalidate_cache()
        tax_grouped = invoice.get_taxes_values()

        # Create new tax lines
        for tax in tax_grouped.values():
            account_invoice_tax.create(tax)