0

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"),
Priyan RockZ
  • 1,605
  • 7
  • 40
  • 68

2 Answers2

5

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.

OmaL
  • 5,037
  • 3
  • 31
  • 48
  • 1
    It should be useful if is_user is defined into res.users object. As per the source, its defined into bpl.officer object. – Priyesh Solanki May 13 '13 at 13:31
  • yep AnomA. need to filter records comparing with another classes's fields also.then how to implement this.? – Priyan RockZ May 14 '13 at 04:10
  • http://stackoverflow.com/questions/6569828/how-to-create-a-dynamic-view-on-openerp please check the answer for this post. – OmaL May 15 '13 at 18:04
  • Hi @OmaL can you please tell me how will i get contact person of company in many2one field or any other way to get it in my custom form pleas help me – Atul Jain Jun 28 '14 at 08:48
  • @AtulJain Add the group different address for sale order then Please check how the delivery address and invoice address is loaded for a sale order. – OmaL Jun 29 '14 at 07:56
  • Hi @Omal Actually i want it in oportunity form there i have tried it but not working fetch all record not filtering it will you please help me – Atul Jain Jun 30 '14 at 12:48
  • i didnt get you.what i said was in sale order, there is 2 fields delivery address and invoice address. Please check the code how the data to these fields are populating – OmaL Jun 30 '14 at 13:38
1

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)
Priyesh Solanki
  • 707
  • 4
  • 6