Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

OpenERP many2one field with function

By
priyankahdp
on 5/13/13, 9:51 AM 9,384 views

I need to create many2one field.but it should need to filter data as per my logic in function.then how to implement this in OpenERP ver 7 ?

i tried with below code.but its not give a list.just load as a readonly field :

def _get_users(self, cr, uid, ids, field_name, arg, context=None):
    res = {}
    users_list=[]
    officer_ids = self.search(cr, uid , 'bpl.officer', [('is_user', '=', True)])
    officer_obj = self.browse(cr, uid, officer_ids, context=context)
    for record in officer_obj:
        users_list.append(record.user_id.id) 
    user_obj = self.pool.get('res.users')
    for data in self.browse(cr, uid, ids, context=context):
        res[data.id] = users_list
    return res

_name = "bpl.officer"
_description = "Officer registration details"
_columns = {
    'bpl_company_id':fields.many2one('res.company', 'Company', help='Company'),
    'bpl_estate_id':fields.many2one('bpl.estate.n.registration', 'Estate', help='Estate', domain="[('company_id', '=', bpl_company_id)]"),
    'bpl_division_id':fields.many2one('bpl.division.n.registration', 'Division', help='Division', domain="[('estate_id','=',bpl_estate_id)]"),
    'name': fields.char('Name', size=128, required=True),
    'is_user': fields.boolean('Is User', help="Is System user or not"),
    'user_id': fields.function(_get_users, type="many2one",relation="res.users"),

need to add more filters for the many2one field.is any another way to implement this .?

priyankahdp
on 5/14/13, 1:30 AM
4

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 5/13/13, 9:56 AM

Hi Of course yes , Fuction field is a readonly field, it just update when record being update

and return only value shown in that field according to method of that field you should use domain in method

no list of value you can select in function many2one field

Thanks

thanks Sandeep :-) then how to add multiple domain filters to my field.

priyankahdp
on 5/13/13, 11:49 AM
0

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 5/13/13, 11:03 AM

If you want to filter data as per your logic then you should call your method from _defaults and make your field fields.many2one instead of fields.function.

Like this:

'user_id': fields.many2one("res.users", "Users"),

_defaults = {
    'user_id': _get_users,
}

And then change the code in your method.

Try this:

def _get_users(self, cr, uid, context=None):
    users_ids = []
    officer_ids = self.search(cr, uid , 'bpl.officer', [('is_user', '=', True)])
    for record in self.browse(cr, uid, officer_ids, context=context):
        if record.user_id:
            users_ids.append(record.user_id.id)
    return users_ids

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 5/13/13, 9:51 AM
Seen: 9384 times
Last updated: 3/16/15, 8:10 AM