我有两个one2many
关系的表。我想在更改master表中的名称值后在slave表中创建一组记录。但我有一个错误:
文件" C:\ odoo80 \ openerp \ addons-custom \ att \ agreement.py",第18行,in _onchange_name
self.pool [' att.agreement.line']。创建({' agreement_id':id,' name':str(x),& #39;数量':x * 100})
文件" C:\ odoo80 \ openerp \ api.py",第250行,在包装器中 返回old_api(self,* args,** kwargs)
TypeError:create()至少需要4个参数(给定2个)
我的课程是:
class Agreement(models.Model):
_name = 'att.agreement'
line_id = fields.One2many('att.agreement.line','agreement_id', 'Lines', copy=True )
name = fields.Char(string='Name', required=True)
def create(self, cr, uid, values, context=None):
return super(Agreement, self).create(cr, uid, values, context)
@api.onchange('name')
def _onchange_name(self):
if (self.name):
for x in range(1,5,1):
self.pool['att.agreement.line'].create({'agreement_id': id , 'name': str(x), 'qty': x * 100} )
class AgreementLine(models.Model):
_name = 'att.agreement.line'
agreement_id = fields.Many2one('att.agreement', ondelete='cascade', select=True ) # required=True ,readonly = True
name = fields.Char(string='Name', required=True)
qty = fields.Integer(string = 'Qty')
def create(self, cr, uid, values, context=None):
id = super(AgreementLine, self).create(cr, uid, values, context)
return id
我的观点是:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- form AGREEMENT -->
<record model="ir.ui.view" id="att_agreement_view_form">
<field name="name">Agreement</field>
<field name="model">att.agreement</field>
<field name="arch" type="xml">
<form string="Agreement">
<sheet>
<group colspan="4">
<group>
<field name="name"/>
</group>
</group>
<notebook>
<page string="Agreement Lines">
<tree string="Agreement Lines" create ='false'>
<field name="line_id" >
<field name = "id"/>
<field name="name"/>
<field name="qty"/>
</field>
</tree>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record model="ir.ui.view" id="att_agreement_view_tree">
<field name="name">List of agreement</field>
<field name="model">att.agreement</field>
<field name="arch" type="xml">
<tree string="List of agreements">
<field name="name"/>
</tree>
</field>
</record>
<!-- window action -->
<record model="ir.actions.act_window" id="att_agreement_list_action">
<field name="name">Agreement</field>
<field name="res_model">att.agreement</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="att_agreement_menuitem" name="Соглашения"
parent="att_menu_agreement"
action="att_agreement_list_action"/>
</data>
</openerp>
有人能帮助我吗?
答案 0 :(得分:0)
您正在混合使用Odoo的API 8和API 7版本。在您的情况下,您正在使用API 8,因此请勿使用pool
。请改用env
:
self.env['att.agreement.line'].create({'agreement_id': id , 'name': str(x), 'qty': x * 100})
答案 1 :(得分:0)
谢谢!我重写了这样的方法:
@api.onchange('name')
def _onchange_name(self):
if self.name:
if isinstance(self.id, models.NewId):
for x in range(1,6,1):
self.line_id.create({'agreement_id': self.id, 'name': str(x), 'qty': x * 100})
我的代码没有运行时错误。但我现在有另一个问题 - 创建了奴隶记录而没有与主记录的链接(协议列表中的 agreement_id 字段为空)。之所以发生这种情况是因为在创建从属记录之前未保存主记录。但是我需要在on_change事件之后创建slave记录,然后将master记录保存在数据库中。相反,如果我在视图上使用列表按钮“添加项目”,我得到一个正确的结果(我看到列表中添加的元素,但数据库中没有任何内容) 我不知道,如何解决这个问题...
P.S。我需要一个像创建奴隶记录的行为,点击按钮“添加项目”,但在程序代码中。
答案 2 :(得分:0)
class Agreement(models.Model):
_name = 'att.agreement'
line_id = fields.One2many('att.agreement.line','agreement_id', 'Lines', copy=True )
name = fields.Char(string='Name', required=True)
@api.model
def create(self, values):
return super(Agreement, self).create( values)
@api.onchange('name')
def _onchange_name(self):
if (self.name):
for x in range(1,5,1):
self.env['att.agreement.line'].create({'agreement_id': self. id , 'name': str(x), 'qty': x * 100} )
class AgreementLine(models.Model):
_name = 'att.agreement.line'
agreement_id = fields.Many2one('att.agreement', ondelete='cascade', select=True ) # required=True ,readonly = True
name = fields.Char(string='Name', required=True)
qty = fields.Integer(string = 'Qty')
@api.model
def create(self, values):
return super(AgreementLine, self).create( values)
答案 3 :(得分:0)
此代码可以完成所有工作: self.line_id + = self.line_id.new({&#39; name&#39;:str(x),&#39; qty&#39;:x}) 强>
@api.onchange('name')
def _on_change_name(self):
if self.name:
for x in range(1,5,1):
self.line_id += self.line_id.new({'name': str(x), 'qty': x})