Forced Selection widget sort order ?

Asked by Jonathan Liuti

Is there a way to force the sort order of entries in such a field ?

My problem here is to sort the "shop" entries alphabetically in the sale order form.
Its originally a many2one field

'shop_id': fields.many2one('sale.shop', 'Shop', required=True, readonly=True, states={'draft': [('readonly', False)]})

That is then "typed" into a selection in the view

<field name="shop_id" on_change="onchange_shop_id(shop_id)" widget="selection"/>

Thanx,
John

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Jonathan Liuti
Solved:
Last query:
Last reply:
Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#1

You should overwrite the read method of the shop object to sort it by
alphabetical order.

Add a context value in

<field name="shop_id" on_change="onchange_shop_id(shop_id)"
widget="selection"/>

and in the overwritten read method if you want to sort shops only for
this view.

Quentin

Le 10/02/2011 11:57, Jonathan Liuti a écrit :
> New question #144830 on OpenERP Server:
> https://answers.launchpad.net/openobject-server/+question/144830
>
> Is there a way to force the sort order of entries in such a field
> ?
>
> My problem here is to sort the "shop" entries alphabetically in
> the sale order form. Its originally a many2one field
>
> 'shop_id': fields.many2one('sale.shop', 'Shop', required=True,
> readonly=True, states={'draft': [('readonly', False)]})
>
> That is then "typed" into a selection in the view
>
> <field name="shop_id" on_change="onchange_shop_id(shop_id)"
> widget="selection"/>
>
> Thanx, John
>

Revision history for this message
Jonathan Liuti (liuti-john) said :
#2

Thx Quentin.

I managed to do this by only adding

_order = "name" in a inherited sale.shop class.

My problem now is to set the default value to the first alphabetical entry and not the first id.

By the way, could you explain a bit more how to overwrite the read method correctly ?

Thanks,
John.

Revision history for this message
Jonathan Liuti (liuti-john) said :
#3

Here is how i extend sale.order:

class sale_order(osv.osv):
 _name="sale.order"
 _inherit = "sale.order"

 def _get_first_alphabetical_shop_id(self, cr, uid, ids, context=None):
  shops = self.pool.get('sale.shop')
  first_shop_id = shops.search(cr, uid, [], limit=1)
  return first_shop_id[0]

 _defaults = {
  'shop_id': _get_first_alphabetical_shop_id,
 }

sale_order()

From my understanding it should create a default value of the first result from the search.
Having set the _order field sooner on the name field, it return the id of the first shop alphabetically sorted (a print shows that its the case).

But still, it uses the id 1 as the default value.

If i try to set the defaults as follow:

 _defaults = {
  'shop_id': lambda *a:3,
 }

It keeps returning shop_id 1 as default...

Any hint ?
Thx.

Revision history for this message
Jonathan Liuti (liuti-john) said :
#4

Ok,

I've flushed the shop entry that was always selected by default, recreated it and now the code above is working ;-)

Mysterious behavior... I suppose it was set as default value somewhere else...
If someone knows about it, I'd be glad to further understand this point.

Bests,
John.