-1

How to filter many2one field in openerp.

_columns = {
   'hello': fields.selection([('1','one'),('2','two')],'hello'),
   'product_id': fields.many2one('product.product',
                                 'Product',
                                 domain=[('type','=',hello)])'
   ...
}

If assume product.product have field called type which is also selection and its value same as hello does it work in xml or python??

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
goblin2986
  • 971
  • 3
  • 16
  • 32
  • Your question could use more detail. Tell us what exactly you're trying to do. Example data really helps clarify the question. – Don Kirkby Mar 01 '11 at 19:57
  • @shahjapan ''hello:fields.selection([('1','one'),('2','two')],'hello'), 'product_id': fields.many2one('product.product', 'Product', domain=[('type','=',hello)])' if assume product.product have field called type which is also selection and its value same as hello does it work in xml or python?? – goblin2986 Mar 03 '11 at 05:42
  • You should edit your question instead of answering with a comment – Loïc Faure-Lacroix Feb 13 '15 at 17:43

4 Answers4

8

you can try the domain attribute of fields.many2one as below

'product_id': fields.many2one('product.product', 'Product', domain=[('purchase_ok','=',True)], change_default=True),

alternative way -> you can provide domain in your XML View as below,

<field name="product_id" domain="[('purchase_ok','=',True)]"/>

shahjapan
  • 13,637
  • 22
  • 74
  • 104
  • ''hello:fields.selection([('1','one'),('2','two')],'hello'), 'product_id': fields.many2one('product.product', 'Product', domain=[('type','=',hello)])' if assume product.product have field called type which is also selection and its value same as hello does it work in xml or python?? – goblin2986 Mar 03 '11 at 05:39
  • I think you want to change the domain of product_id (M2O) based on another field `hello` if so you need to write an `on_change` attribute on field `hello` in your xml and in python write respective function `onchange_hello` should return appropriate `domain` for field `product_id`, for detailed example see the existing module purchase -> method `product_id_change` – shahjapan Mar 03 '11 at 06:58
1

You need to specify the domain in the view file which you want the many to one field to be filtered. For e.g. if you want to filter suppliers from the customers list just give the following in the view.

:domain="[('supplier','=',True)]"
MarkoHiel
  • 15,481
  • 2
  • 21
  • 29
Pravitha V
  • 3,308
  • 4
  • 33
  • 51
0

I think u want to filter product.product according to hello fields right. so write the onchange method on hello filed in xml and in method, filter the product id which type is match to hello filed value.

In function/method u can add all the ids which type is matches to hello filed value, in the list and after return this list to product_id.

Anup
  • 200
  • 1
  • 13
  • how to write it when Domain filter with more than 2 conditions ? – Priyan RockZ Apr 29 '13 at 10:20
  • 1
    yes, it will work for one and more than one condition, bcz we generating list of ids which are satisfied all the condition which we want, and this list will return... – Anup Jun 05 '13 at 08:38
0

Try the context or domain parameters described in the developer book. I haven't used them, but you can probably find examples in the core modules. The best description of domain syntax is on the orm.search() method.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286