Skip to main content
 首页 » 编程设计

python之如何将列表中的数据放入many2many

2025年01月19日14落叶

我想把数据表单列表放到many2many

@api.multi 
def favorite_menus(self): 
...... 
return popular_words[:3] 

我尝试显示尽可能多的字段 (popular_word 中的 deta 是 [a,b,c])

menu = fields.Many2many(comodel_name='lunch.order.line', default=lambda self:[self.favorite_menus()]) 

请您参考如下方法:

在 odoo 关系字段中接受一个命令列表,所以如果你的列表是一个命令列表,我认为它应该是好的:

默认用于创建方法:

添加记录使用 4 cammond 你的列表应该包含这样的数据:

命令4告诉odoo把id添加到记录列表的第二位

      [(4, id), (4, id2), .......] 

id 是您要添加为整数值的记录的 ID。

如果你想创建一个新记录使用 0 命令:

         [(0, 0, {'field_name': value_name, 'field_2': value_2 }) , ...] 

命令 0 告诉 odoo 从字典的最后位置创建一条记录

你不需要默认值,而是需要一个默认域:

menu = fields.Many2many( 
    comodel_name='lunch.order.line', 
    domain=lambda self:self.favorite_menus()) 
 
@api.multi 
def favorite_menus(self): 
    """ return a default domain.""" 
    # first calculate the list of the menu that you want 
    # the user to select one of them i'm assuming that you use search 
    # so the result is a recordSet of menu  
    menus = ..... 
 
    return "[('id', 'in', '%s')]" % (tuple(menus.ids), ) # keep the comma  
    # or try this i think bouth work 
    return [('id', 'in', menus.ids)] 

注意:您可能需要更改域以满足您的需要。 但要防止用户选择您需要过滤的其他元素 他们在 m2m 或 o2m 或 m2o 领域中使用域。