Skip to Content
Menu
This question has been flagged
2 Replies
15350 Views

Hello all,

I am trying to perform a filter/search through a wizard for a One2manyfield. The One2manyfield connects to the model stock.picking. I created a TransientModel in which a user can pick a Sale Order and the wizard should return a form with a filtered one2many field. The form will then only show the pickings which have the Sale Order that the user selected as Origin. The TransientModel looks like this:

class SearchPacking(models.TransientModel):
    _name = 'search.packing'
_description = 'Search a packing'

sale_order_id = fields.Many2one('sale.order',string="Sale Order")

def search_filtered_packing(self):
return {
'type':'ir.actions.act_window',
'view_type':'form',
'view_mode':'form',
'src_model':'search.packing',
'res_model':'pickpack.packview',
'target':'new',
'res_id':self.env.context.get('active_id'),
'domain':[('origin','=',self.sale_order_id)]
}


The model pickpack.packview is a model that shows all the pickings with picking type "Pack" in a batch. It has a one2many field that looks like this:

class PackView(models.Model):
    // OTHER DEFINITIONS ​   ​    ​   ​     ​   ​    ​   ​
    pickings = fields.One2many('stock.picking','packviews',string='Operations to pack',readonly=False,store=True, compute='_compute_operations_to_pack')
@api.one
@api.depends('batch_id')
def _compute_operations_to_pack(self):
self.ensure_one()
res = self.env['stock.picking']
for picking in res.search([('batch_id','=',self.batch_id.id),('picking_type_id.name','=','Pack')]):
res += picking
self.pickings = res

What happens now if I click the button in the wizard that calls search_filtered_packing (after selecting a Sales Order) is that I just get the same model back, without any filters. 
The One2Many field is searchable on Sales Order in the tree view. Can anyone see what is going wrong here?


Thanks in advance.

Avatar
Discard
Best Answer

Hi,

There are some problems in your solution:

  • If you really mean to compute the One2many field, you should not define it with the inverse_name. By doing this, there are no real connection between these 2 models, and you cannot store this o2m field. If the relation already exist between 2 model with the m2o relation defined in the corresponding model, then you can only apply domain to decide what to be shown.

  • You cannot domain a form view, but you can domain a field shown as tree in the form view, if you return correct value to domain that field in particular.

Then the solution now is depends on what you want to achieve:

  • You can add a sale_order_origin to stock.picking form view to apply domain dynamically to the related one2many field, with an onchange function.

  • If you want to use the wizard, then from that wizard, you select the reference Sale Order, then assign the result recordset to an m2m field on the wizard, and then finally write those m2m ids to the o2m field in stock.picking.

I think you would want to domain what to be picked base on the Sale Order (delivery operation) rather than automatically creating o2m record from the wizard. Then returning domain for a selected field using onchange function is your best friend. Try :

return {'domain':{'picking_ids': [('origin', '=', self.sale_order_id.name)]}}
Avatar
Discard
Best Answer

You return a form view and, moreover, you return res_id in your method. So, you say to Odoo - 'show me a form of this object in a new window'. The param domain is applied to tree, kanban, calendar views: so to any where there are a few items.

A few other comments:

  • How might origin (char) be equal to sale_order_id (recordset)?

  • What for do you need 'src_model'?

Thus, you should re-develop your action to return, for example a list. Something like:

@api.multi
def search_filtered_packing(self):
self.ensure_one()
return {
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'pickpack.packview',
'type': 'ir.actions.act_window',
'domain':[('origin', '=', self.sale_order_id.name)],
}
Avatar
Discard
Related Posts Replies Views Activity
1
Jun 24
442
2
Dec 20
5001
1
Nov 18
2858
1
Feb 16
4896
1
Sep 23
2218