selection parameter in many2one field doesn't work

Asked by Digitel

Version 5.0.9
When trying to use the selection parameter on a field, the result is not filtered at all.
In the example mentioned below, all extensions are present when having to chose the extension and not only the one returned by the _sel_extension function

class asterisk_user(osv.osv):

    def _sel_extension(self, cr, uid, context={}):
        obj = self.pool.get("asterisk.extensions")
        ids = obj.search(cr, uid, [('type','=','sip_extension')], context=context)
        res = obj.read(cr, uid, ids, ['name','id'], context=context)
        res = tuple([(r['id'],r['name']) for r in res])
        return res

    _name = "res.users"
    _inherit = "res.users"
    _columns = {
        "extension":fields.many2one('asterisk.configuration.file.category','extension',selection=_sel_extension),
    }
asterisk_user()

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Jay Vora (Serpent Consulting Services)
Solved:
Last query:
Last reply:
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) said :
#1

Hello Fabien Toune,

You cannot use selection and many2one altogeher for the same field.

As far as what I see, the solution for your problem is:

 "extension":fields.many2one('asterisk.configuration.file.category','extension'),
And in the View,
<field name="extension" widget="selection"/>

Moreover, you did a mistake by specifying diffeent table references to same field. You specified asterisk.configuration.file.category for the field (it will be set as a foreign key) but you are willing to specify asterisk.extensions on display!

May I know what is the purpose of this?
Thanks.

Revision history for this message
Digitel (fabien-toune-digitel) said :
#2

Hello,

Thank you for the answer,

The difference between table reference is a typo (cut and paste problem), it's is actually asterisk.configuration.file.category in both places (object definition and function spec)

The point in this is to filter the choices proposed in the widget (widget or many2one) to only the categories with type=field. I actually achieved this goal by specifying a filtered domain in the action.act_window.

Just wanted to follow what you mention in the documentation, and which I think was working in the past :

Textually : Using relation fields many2one with selection. In fields definitions add:

http://doc.openerp.com/developer/2_5_Objects_Fields_Methods/field_type.html

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) said :
#3

Hello,

You may use "extension":fields.many2one('asterisk.configuration.file.category','extension', domain=[('field','operator','value')]),.

If this has been solved, can you please mark this as solved?

Thanks.

Revision history for this message
Digitel (fabien-toune-digitel) said :
#4

Hello,

Thank you for your answer,

Can you please confirm that it's no more possible to do what is mentioned in the documentation (using many2one with selection), I was actually planning to use an external python function to enhance the filter by only showing online extensions...
If this doesn't work, could I use a function returning the list of tuples used by the named domain argument ?

Sorry to make this last, I will make this solved whatever your answer is :-D

Revision history for this message
Best Jay Vora (Serpent Consulting Services) (jayvora) said :
#5

Hello,

You may use fields.selection itself to serve your aim.

Or

"extension":fields.many2one('asterisk.configuration.file.category','extension', domain=[('field','operator','value')]), to select specific records.

I confirm that, many2one with selection is only possible via widget="selection" on view definition : <field name="extension" widget="selection"/>.

You cannot use "extension":fields.many2one('asterisk.configuration.file.category','extension',selection=_sel_extension),.

If you want to use external python function , you may opt for:
'value': fields.selection(_sel_selection, 'Value',)
And the _sel_selection() method performs desired operation and it returns set of tuples in list.

Thanks.

Revision history for this message
Digitel (fabien-toune-digitel) said :
#6

Great, thank you very much for this answer which completely answer my questions :-D

Revision history for this message
Juan Pablo Rabino (jprabino) said :
#7

Dear Jay, I'm running on OpenERP 6.1. Try to filter the selection according to group (I have several possible "status" for each record and I want that only some user can set some status values, and not others). But if you try and do what is on the specification and also set the view widget on "selection" it shows the all the status with no filtering at all.

Revision history for this message
Serpent Consulting Services (serpent-consulting-services) said :
#8

Juan,

You can use domain to filter the choices.

Normally, status fields are not meant to be changed by end user. They should rather be altered by either automated action or button calls.

Thanks.

Revision history for this message
Juan Pablo Rabino (jprabino) said :
#9

Sorry, but could you please illustrate on the use of domain, it is not very well documented.

Yes, I actually was going for the use of buttons in the first place, but since there are groups of user that can set 4 or 5 different status for the same record, I didn't want to populate the interface with so many buttons.

Revision history for this message
Serpent Consulting Services (serpent-consulting-services) said :
#10

Juan,

Let me detail something here:

The domains are just premature conditions which will be where clause for the querying. Example [('name','=','SerpentCS')] arguably means to search only those ids where name='SerpentCS'. Hope this clarifies a bit.

You can always make use of the buttons smartly. Its not necessary to show all buttons at the same time.
For example, you can study the case of Sale Order view and you might get a better hint.

Hope this helps.

Regards,
Serpent Consulting Services.