I need a many2many(product_product_ids) filled based on search result. For example, I have a search button defined on wizard view(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:
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).
Does anyone have a suggestion?
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 }) 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)
See more about Many2many
default_get :
Returns default values for the fields in fields_list. default_get method is called when your wizard/form is load, you need to override this method to do this.
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',])
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.
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
for your method search_test(), change the return to
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,
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With