简体   繁体   中英

OpenERP many2one field with function

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"),

First of all the functional field you have created is wrong.user_id itself is a functional field any you are using some magic in it(i dont understand). Please change it to a many2one field.

If you want to filter out some records, you can add domain filter in your xml file where you add the user_id field. for example <field name="user_id" domain="[('is_user', '=', True)]"/> if is_user is a field in res_partner table.otherwise you can over ride the fields_view_get function and add the specify the domain from there.

First, make user_id a many2one field with res.users.

You can add a context and override search method on the base of that context of res.users object and return your records as per your logic.

As there is no way to add direct domain, I think You need to override search method of res.users on the base of context like this:

def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
        if context is None:
            context = {}
        users_list = []
        if context.get('test') == 'test':
            officer_ids = OFFICE_OBJ.search(cr, uid , [('is_user', '=', True)])
            officer_obj = OFFICE_OBJ.browse(cr, uid, officer_ids, context=context)
            for record in officer_obj:
                users_list.append(record.user_id.id)
                return users_list
        return super(res_users, self).search(cr, uid, args, offset, limit,
                order, context=context, count=count)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM