简体   繁体   English

如何根据ids搜索(向导)填充many2many字段

[英]How to populate many2many field based on ids search(wizard)

I need a many2many(product_product_ids) filled based on search result. 我需要根据搜索结果填充many2many(product_product_ids)。 For example, I have a search button defined on wizard view(search_test): 例如,我在向导视图(search_test)上定义了一个搜索按钮:

<group>
    <field name="quantity"/>
    <field name="product_product_ids"/>
</group>
 <footer>
    <button name="search_test" type="object" string="Search" class="oe_highlight"/>
       or
    <button string="Cancell" class="oe_link" special="cancel"/>
 </footer>

In wizard model, I have defined these fields and functions: 在向导模型中,我已经定义了这些字段和函数:

class sale_order_add_balerce(models.TransientModel):
    _name = 'sale.order.add_balerce'
    _description = 'Sale order add balerce'

    _columns = {
    'product_product_ids': fields.many2many('product.product', string='Products'),
    'quantity' : fields.float('Quantity', default='1.0')                
}
    def search_test(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        product_obj=self.pool.get('product.product')
        #search process
        product_ids_list =  product_obj.search(cr, uid, [], context=context)
        print product_ids_list
        #populating many2many field
        self.write(cr, uid, ids, {'product_product_ids': (6, 0, [product_ids_list])})
        return {
            'res_model': 'product.product',
            'type':'ir.ui.view',
            'context': context, 
            'res_id': ids[0] #open wizard again
        }                

In line 排队

self.write(cr, uid, ids, {'product_product_ids': (6, 0, [product_ids_list])})

I attempt to update many2many field after search process, but nothing happens and I see no errors I have also tried with these variants: 我尝试在搜索过程后更新many2many字段,但没有任何反应,我发现没有错误我也尝试过这些变体:

self.write(cr, uid, ids, {'product_product_ids': (0, 0, [product_ids_list])})

self.create(cr, uid,{'product_product_ids': (6, 0, [product_ids_list])})

self.create(cr, uid, ids, {'product_product_ids': (0, 0, [product_ids_list])})

However, I still don't get my many2many field filled(I don't see any changes in view). 但是,我仍然没有填充我的many2many字段(我看不到任何视图变化)。

Does anyone have a suggestion? 有没有人有建议?

for your method search_test(), change the return to 对于您的方法search_test(),将返回更改为

return  {
            'name': 'Name for your window',
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'sale.order.add_balerce',
            'target': 'new',
            'res_id': ids[0],
            'context': context,
        }
  • One2many and Many2many use a special "commands" format to manipulate the set of records stored in/associated with the field. One2many和Many2many使用特殊的“命令”格式来操纵存储在字段中/与字段相关联的记录集。

This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. 此格式是按顺序执行的三元组列表,其中每个三元组是在记录集上执行的命令。 Not all commands apply in all situations. 并非所有命令都适用于所有情况。 Possible commands are: 可能的命令是:

(0, _, values) (0,_,值)

adds a new record created from the provided value dict. 添加从提供的值dict创建的新记录。

(1, id, values) (1,id,值)

updates an existing record of id id with the values in values. 使用值中的值更新id id的现有记录。 Can not be used in create(). 不能在create()中使用。

(2, id, _) (2,id,_)

removes the record of id id from the set, then deletes it (from the database). 从集合中删除id id的记录,然后删除它(从数据库中)。 Can not be used in create(). 不能在create()中使用。

(3, id, _) (3,id,_)

removes the record of id id from the set, but does not delete it. 从集合中删除id id的记录,但不删除它。 Can not be used on One2many. 不能在One2many上使用。 Can not be used in create(). 不能在create()中使用。

(4, id, _) (4,id,_)

adds an existing record of id id to the set. 将id id的现有记录添加到集合中。 Can not be used on One2many. 不能在One2many上使用。

(5, _, _) (5,_,_)

removes all records from the set, equivalent to using the command 3 on every record explicitly. 从集合中删除所有记录,相当于在每条记录上明确使用命令3。 Can not be used on One2many. 不能在One2many上使用。 Can not be used in create(). 不能在create()中使用。

(6, _, ids) (6,_,ids)

replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids. 替换ids列表中集合中的所有现有记录,相当于使用命令5,后跟命令4,用于id中的每个id。 Can not be used on One2many. 不能在One2many上使用。

Note 注意

Values marked as _ in the list above are ignored and can be anything, generally 0 or False. 上面列表中标记为_的值将被忽略,可以是任何内容,通常为0或False。

Many2many Many2many

For a many2many field, a list of tuples is expected. 对于many2many字段,预计会有一个元组列表。 Here is the list of tuple that are accepted, with the corresponding semantics. 这是接受的元组列表,以及相应的语义。

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary (0,0,{values})链接到需要使用给定值字典创建的新记录

(1, ID, { values }) update the linked record with id = ID (write values on it) (1,ID,{values})使用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) (2,ID)删除并删除id = ID的链接记录(在ID上调用unlink,这将完全删除对象,以及指向它的链接)

(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) (3,ID)使用id = ID剪切到链接记录的链接(删除两个对象之间的关系但不删除目标对象本身)

(4, ID) link to existing record with id = ID (adds a relationship) (4,ID)链接到id = ID的现有记录(添加关系)

(5) unlink all (like using (3,ID) for all linked records) (5)取消全部链接(如使用(3,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,[ID])替换链接ID列表(如使用(5)然后(4,ID)ID列表中的每个ID)

See more about Many2many 了解更多关于Many2many的信息

default_get : default_get

Returns default values for the fields in fields_list. 返回fields_list中字段的默认值。 default_get method is called when your wizard/form is load, you need to override this method to do this. 加载向导/表单时调用default_get方法,您需要覆盖此方法才能执行此操作。

Syntax: 句法:

default_get(self, cr, uid, fields_list, context=None):

Parameters: 参数:

fields_list (list) : list of fields to get the default values for (example ['field1', 'field2',]) fields_list(list) :获取默认值的字段列表(example ['field1','field2',])

Returns: 返回:

dictionary of the default values (set on the object model class, through user preferences, or in the context) 默认值的字典(在对象模型类上设置,通过用户首选项或在上下文中设置)

Solution: 解:

And finally your solution is overriding default_get method to set default value for many2many field. 最后,您的解决方案将覆盖default_get方法,以设置many2many字段的默认值。

def default_get(self,cr,uid,fields,context=None):
        res = super(sale_order_add_balerce, self).default_get(cr, uid, fields, context=context)
        product_obj=self.pool.get('product.product')
        product_ids_list =  product_obj.search(cr, uid, [], context=context)
        res["product_product_ids"] = [(6,0,[product_ids_list])]
        return res    

Replace this code 替换此代码

self.write(cr, uid, ids, {'product_product_ids': (6, 0, [product_ids_list])})

to

self.write(cr, uid, ids, {'product_product_ids': [(6, 0, [product_ids_list])]})

Only you have syntax mistake to write ids on many2many field. 只有语法错误才能在many2many字段上写入id。 It will take list of tuple as argument. 它将把元组列表作为参数。 General syntax is 一般语法是

'many2many_fieldname': [(6, 0, [list_of_ids])] 

For more help you may visit many2many field document. 如需更多帮助,您可以访问many2many现场文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM