Skip to Content
Menu
This question has been flagged
1 Reply
3095 Views

Server action:
env.cr.execute( "select id from stock_move where location_id = location_dest_id;" )
stock_moves = env.cr.fetchall()
raise Warning(stock_moves)​

***
Example of a failing filter!

​['|', ['location_id', 'ilike', location_dest_id ], ['location_dest_id', 'ilike', location_id ] ]

Would it be possible to use SQL in a Filter? How to solve it...?

Avatar
Discard
Best Answer

hi this is an example of mine , on how to use cr to execute a sql query :)

in this example i'm trying to get a quantity available of a given product in a given location : 

@api.one
def get_qty(self, product_id, location_name):
cr=self._cr
query = """ SELECT sum(quant.qty)
FROM stock_quant quant, stock_location loc
WHERE loc.id = quant.location_id AND (loc.complete_name like %s)
group by quant.product_id
having quant.product_id = %s
;
"""
cr.execute(query,(location_name,product_id,))
b=cr.fetchone()
if b == None:
return 0
else:
return int(b[0])

Here an example of using it :

product_id = 10
location = '% WH%'
qty_available = self.get_qty(product_id,location)

Dont forget to upVote :)


Avatar
Discard
Author

Thank you! I must have expressed myself bad... Let me try again!

***

Is there a way to make a filter in Odoo 8 asking if two columns are the same?

Oh you are talking about domain filtering with multiple conditions ?

Author

Yes! :-) The server action above is what I want! :-)

"select id from stock_move where location_id = location_dest_id;"

i appreciate if you share more code to make it easy for us to help you :)

like field declaration from python and xml file

Author

I made a server action and it's working!

Thank you for assistance!

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

env.cr.execute( "select id from stock_move where location_id = location_dest_id;" )

# raise Warning( [ item[0] for item in env.cr.fetchall() ] )

ids = [ item[0] for item in env.cr.fetchall() ]

action = env['ir.actions.act_window'].for_xml_id('stock', 'action_move_form2')

action.update({

'domain': [('id', 'in', ids )],

'context': {},

})

#raise Warning ( env.cr.fetchall() )

#raise Warning( action )

so your problem is , how to make a sql query that match that condition ​['|', ['location_id', 'ilike', location_dest_id ], ['location_dest_id', 'ilike', location_id ] ],

in other meaning : you want to filter the stock mouvements, and show every mouvement except the mouvements that has location_id same as location_dest_id .?