没有设置odoo one2many默认值 [英] odoo one2many default not set
问题描述
我编写了一个向导,该向导的表单视图应显示一个one2many字段,其中的行取自 context ['active_ids'].
I wrote a wizard which form view should show a one2many field with rows taken from context['active_ids'].
我正确地将one2many设置为默认值,但是当表单打开时,没有显示行.
I set the one2many default correctly, but when the form opens, no rows are showed.
我错过了什么吗? (对于代码缩进,我深表歉意)
Did I miss anything? (I apologize for code bad indentation)
class delivery_wizard(models.TransientModel):
_name = 'as.delivery.wizard'
address = fields.Many2one('res.partner')
details = fields.One2many('as.delivery.detail.wizard', 'delivery')
carrier = fields.Many2one('delivery.carrier')
@api.model
def default_get(self, fields_list):
res = models.TransientModel.default_get(self, fields_list)
ids = self.env.context.get('active_ids', [])
details = self.env['as.delivery.detail'].browse(ids)
dwz = self.env['as.delivery.detail.wizard']
dws = []
for detail in details:
dw = dwz.create({
'production': detail.production_id.id,
'quantity': detail.quantity,
'actual_quantity': detail.quantity,
'enabled': detail.production_id.state == 'done',
'delivery': self.id,
})
dws.append(dw.id)
res['details'] = [(6, False, dws)]
res['address'] = details[0].delivery_id.address_id.id
return res
class delivery_detail_wizard(models.TransientModel):
_name = 'as.delivery.detail.wizard'
production = fields.Many2one('as.production')
quantity = fields.Float()
actual_quantity = fields.Float()
force = fields.Boolean()
enabled = fields.Boolean()
delivery = fields.Many2one('as.delivery.wizard')
推荐答案
可能存在问题:
res['details'] = **[(6, False, dws)]**
您的详细信息字段是一个One2many字段,[(6,0,[IDS])]用于Many2many. 在您的情况下,您无需将任何内容分配给details字段;它是一个One2many,因此它是自动的,因为您已经创建了相应的Many2one记录(dw).
Your details field is a One2many field, [(6,0, [IDS])] are for Many2many. In your case, you don't need to assign anything to the details fields ; it's a One2many, so it's automatic as you already created the corresponding Many2one record (dw).
- 对于Many2many
对于many2many字段,应该有一个元组列表.这是清单 具有相应语义的元组
For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics
(0,0,{values})链接到需要创建的新记录 用给定的值字典
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1,ID,{values})用id = ID更新链接记录(写 上的值)
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2,ID)删除和删除ID =的链接记录 ID(调用ID上的取消链接,这将完全删除对象,并且 以及它的链接)
(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)剪切到id = ID的链接记录的链接 (删除两个对象之间的关系,但不删除 目标对象本身)
(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)链接到ID = ID的现有记录(添加一个 关系)
(4, ID) link to existing record with id = ID (adds a relationship)
(5)取消所有链接(例如对所有链接使用(3,ID) 记录)
(5) unlink all (like using (3,ID) for all linked records)
(6,0,[IDs])替换链接的ID列表(例如使用(5) 然后(4,ID)代表ID列表中的每个ID)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
示例:[[(6,0,[8,5,6,4]))]将many2many设置为ids [8,5,6, 4]
Example: [(6, 0, [8, 5, 6, 4])] sets the many2many to ids [8, 5, 6, 4]
- 还有一个One2many:
(0,0,{values})链接到需要创建的新记录 用给定的值字典
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1,ID,{values})用id = ID更新链接记录(写 上的值)
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2,ID)删除和删除ID =的链接记录 ID(调用ID上的取消链接,这将完全删除对象,并且 以及它的链接)
(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)
示例:[(0,0,{'field_name':field_value_record1,...}),(0,0, {'field_name':field_value_record2,...})]
Example: [(0, 0, {'field_name':field_value_record1, ...}), (0, 0, {'field_name':field_value_record2, ...})]
另外,对于Many2One/,请尝试遵循 odoo指南如果您希望其他人可以轻松理解您的代码,则可以使用One2many很多字段:
Also, try to follow odoo guidelines for Many2One/One2many fields if you want your code to be easily understandable by other people :
One2Many和Many2Many字段应始终以 _ids 作为后缀(例如:sale_order_line_ids)
One2Many and Many2Many fields should always have _ids as suffix (example: sale_order_line_ids)
Many2One字段的后缀应为 _id (例如:partner_id,user_id,...)
Many2One fields should have _id as suffix (example : partner_id, user_id, ...)
这篇关于没有设置odoo one2many默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!