How to fill a lot of many fields based on ids search (wizard)

I need a lot of many (product_product_ids) populated based on the search result. For example, I have a search button defined in the wizard window (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 the wizard model, I 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 am trying to update a lot of many fields after the search process, but nothing happens and I see no errors. I also tried with these options:

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 do not fill out my many many fields (I do not see any changes in the view).

Does anyone have a suggestion?

+4
source share
4 answers

Many2many

many2many . , .

(0, 0, {values}) ,

(1, ID, {values}) id = ID ( )

(2, ID) id = ID ( , )

(3, ID) id = ID ( , -)

(4, ID) id = ID ( )

(5) (, (3, ID) )

(6, 0, [IDs]) (, (5), (4, ID) )

Many2many

default_get:

field_list. default_get , / , .

:

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

:

fields_list (list): ( ['field1', 'field2',])

:

( , )

:

, , 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    
+2

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,
        }
+3
  • One2many Many2many , /, .

, , . . :

(0, _, )

, dict.

(1, id, values)

id id . create().

(2, id, _)

id , ( ). create().

(3, id, _)

id id , . One2many. create().

(4, id, _)

id id . One2many.

(5, _, _)

, 3 . One2many. create().

(6, _, ids)

, 5, 4 . One2many.

, _ , , 0 False.

+2

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

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

. .

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

For more help, you can visit many2many field document.

0
source

Source: https://habr.com/ru/post/1609516/


All Articles