domain is not working with widget="selection"

Bug #695089 reported by Cristian Salamea
22
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Odoo Server (MOVED TO GITHUB)
Won't Fix
Wishlist
Unassigned

Bug Description

I get a M2O field:
 'myfield_id': fields.many2one('object', 'Field', domain="['type','=','foo']"),
 in view get <field name="myfield_id" widget="selection"/>

The selection widget dont restrict the data, remove the widget="selection" domain works fine.

Regards,

Revision history for this message
Cristian Salamea (ovnicraft) wrote :
Revision history for this message
Cristian Salamea (ovnicraft) wrote :
Changed in openobject-client:
status: New → Won't Fix
affects: openobject-client → openobject-server
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Something just went wrong with the project and statusses.
Setting the bug for the radar of versions late to 6.0.
Thanks.

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

For the record, closing this bug report for trunk because it's not a bug, it's a problem of understanding how domains and selection widgets work:

1. Selection widgets (for selection fields or m2o fields with widget=selection) cannot have dynamic domains at all. Their values are statically determined by the server when the view is opened (when fields_view_get is called), and the domain is evaluated at that moment, so it cannot be dynamic.

2. What is a dynamic vs a static domain? When defining a m2o domain at the model level (domain parameter for the fields.many2one constructor), the domain can be passed as a string or as a Python list. When passing as a Python list, the domain is STATIC (it's constructed with only literals) and can be evaluated server-side, specifically to initialize the possible values if widget=selection is used.
When passed as a string, the domain is DYNAMIC and never evaluated server-side because it can contain field names to represent the value of other fields in the view (not only literals), so it is meant to be evaluated on client-side.
For the case of a m2o with widget=selection, the domain should ALWAYS be passed as a Python list (STATIC), because dynamic domains are not supported, so giving a string (client-side) domain will be IGNORED.

I hope this makes things very clear. And to be 100% sure, let's take an example: imagine I make a m2o field to select a Partner, that I would like to display as a selection widget that only allows selecting Customers, not Suppliers.
I could write 3 versions, but only the first one will do what I want:

A.
 at model level:
     'partner_id': fields.many2one('res.partner', 'Customer', domain=[('customer', '=', True)])
 at view level:
   <field name="partner_id" widget="selection"/>
=> GOOD!

B.
 at model level:
     'partner_id': fields.many2one('res.partner', 'Customer', domain="[('customer', '=', True)]")
 at view level:
   <field name="partner_id" widget="selection"/>
=> BAD - notice that domain is a string! - this will NOT work! (as explained above)

C.
 at model level:
     'partner_id': fields.many2one('res.partner', 'Customer')
 at view level:
   <field name="partner_id" widget="selection" domain="[('customer', '=', True)]"/>
=> BAD too - it is equivalent to the previous one

Let me repeat: the reason why B and C will not work is because they are using a client-side DYNAMIC domains, which is not supported for selection widgets (m2o or selection fields). A dynamic client-side domain is sometimes useful for normal m2o fields (widget=many2one) to do something like:
   <field name="partner_id" domain="[('category_id', '=', category_id)]"/>
This allows selecting a partner from the category selected in the other "category_id" field in the form view. As this depends on the dynamic value of a client-side field, it cannot be supported by the server-side when the view is initially opened. That is why dynamic domains are simply ignored for selection widgets.

If this is still not clear, please read again the examples or have a look at the ORM code until you fully understand :-)

Changed in openobject-server:
assignee: OpenERP's Framework R&D (openerp-dev-framework) → nobody
no longer affects: openobject-server/5.0
no longer affects: openobject-client/5.0
no longer affects: openobject-client/trunk
no longer affects: openobject-server/trunk
Revision history for this message
Muhammad Arsalan Ali Khan (m-arsalan-khan) wrote :

What is the difference b/w Example A and Example B ?? :S

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.