写一个按钮从表中复制数据(Python3、Odoo 14)
Posted
技术标签:
【中文标题】写一个按钮从表中复制数据(Python3、Odoo 14)【英文标题】:Write a button to copy data from table (Python3、Odoo 14) 【发布时间】:2021-09-24 08:02:44 【问题描述】:我尝试编写一个复制函数来将数据从表复制到同一个表。
def formmst_copy_func(self):
_logger.info('=== formmst_copy_func start ===')
for record in self:
formbas_obj = self.env['hre.formmst']
defaults =
'form_no': record.form_no,
'hre_orgbas_id': record.hre_orgbas_id.id if record.hre_orgbas_id else False,
'form_name': record.hre_formmst_id.form_name,
'form_type': record.hre_formmst_id.form_type,
'calculate_type': record.hre_formmst_id.calculate_type,
'total_score': record.hre_formmst_id.total_score,
'hre_formdtl_ids': record.hre_formmst_id.hre_formdtl_ids,
formbas_obj.create(defaults)
_logger.info('=== formmst_copy_func end ===')
hre_formmst_id 是 Many2one 字段。 hre_formdtl_ids 是 One2many 字段。
使用按钮调用此函数。
<record id="form_hre_formbas_copy_wizard" model="ir.ui.view">
<field name="name">form.hre.formbas.copy.wizard</field>
<field name="model">hre.formbas.copy.wizard</field>
<field name="arch" type="xml">
<form>
<group col="4">
<field name="hre_formmst_id"/>
<newline/>
<field name="form_no"/>
<field name="hre_orgbas_id"/>
</group>
<footer>
<button string="Copy" name="formmst_copy_func" type="object" class="oe_highlight"/>
or
<button string="Cancel" special="cancel" type="object" class="oe_link"/>
</footer>
</form>
</field>
</record>
该函数可以成功复制数据。 但是原始数据中的hre_formdtl_ids消失了。 如何修复该功能?请给我一些建议。谢谢!
【问题讨论】:
【参考方案1】:尝试将 one2many 字段更新为 [(6, 0, ids)] 或 [(4, id)]
用于填充 many2many 和 one2many:
(0, 0, values ) link to a new record that needs to be created with the given values dictionary
(1, ID, values ) update the linked record with id = ID (write *values* on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
所以你可以试试:
def formmst_copy_func(self):
_logger.info('=== formmst_copy_func start ===')
for record in self:
formbas_obj = self.env['hre.formmst']
defaults =
'form_no': record.form_no,
'hre_orgbas_id': record.hre_orgbas_id.id if record.hre_orgbas_id else False,
'form_name': record.hre_formmst_id.form_name,
'form_type': record.hre_formmst_id.form_type,
'calculate_type': record.hre_formmst_id.calculate_type,
'total_score': record.hre_formmst_id.total_score,
'hre_formdtl_ids': [(4, l.id for l in record.hre_formmst_id.hre_formdtl_ids)],
formbas_obj.create(defaults)
_logger.info('=== formmst_copy_func end ===')
【讨论】:
你的答案是正确的,但是如果是 [(4, ID)] 它只需要一个 id,所以我认为应该是这样的:[(4, l.id) for l in record.hre_formmst_id.hre_formdtl_ids]
。我也更喜欢:[(6, 0, [l.id for l in record.hre_formmst_id.hre_formdtl_ids])]
我曾尝试使用 (4, ID) 和 (6, 0, [IDs]),但是原始数据中的 hre_formdtl_ids 仍然消失了。以上是关于写一个按钮从表中复制数据(Python3、Odoo 14)的主要内容,如果未能解决你的问题,请参考以下文章
MYSQLI 从表中获取结果:显示所有用户(但首先显示豪华和高级用户)? [复制]