Is it possible to change domain of the field at run time?

Asked by KAKA1.2

I want to change domain value of company_id field change with respect user ,if he is parent company user ,he can see all child companies,can u check this code..?,please help me for solution?

def _default_company(self, cr, uid, context={}):
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        if user.company_id:
            company_id = user.company_id.id
        else:
            company_id = self.pool.get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        company_ids = self.pool.get('res.company')._get_company_children(cr, uid, user.company_id.id)
        #_columns['company_id']['domain'] = "[('id','in',"+str(company_ids) +")]"
        return "[('id','in',"+str(company_ids) +")]"

_columns = {
        'company_id': fields.many2one('res.company', string='Company', domain = ),
    }

Question information

Language:
English Edit question
Status:
Answered
For:
Odoo Addons (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#1

Hello Satheesh,

Create Two group and assign two different report rules.One is allow child company and one is allow only selected company only.

For creating groups and record rules use following menus:
Administration/Users/Groups
Administration/Security/Record Rules.

After creating record rules you will assign it to particular group.('Rules Tab' =>Many2many field.)
Assign the group in res.users form, (Administration/Users/Users = > Groups Tab => Many2many field.)

Hope this will help you.
Thanks.

Revision history for this message
KAKA1.2 (satheesh) said :
#2

This is new information ,Thank you

this is working in tree view structure , I want in the form when the selection of the company on clicking search image that shows only if he is parent company user ,he can see all child companies else he see only user assigned company

this shows one company .
columns = {
        'company_id': fields.many2one('res.company', string='Company', domain = [('id','in',1)] ),
    }

  is it possible changed to variable?..

Can u please help me

Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#3

Hello Satheesh,

How can you say that record rules working for tree view only. Record rules are working for all records which are showing in any view (tree or form or graph.).

Thanks.

Revision history for this message
KAKA1.2 (satheesh) said :
#4

Example WAREHOUSE

add record rule condition [u'|', (u'company_id', u'child_of', [4]), (u'company_id', u'=', False)]

in the form when the selection of the company on clicking search image that shows all companies ,we want see only user assigned company

Can u please help me

Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#5

Hello Satheesh,

You can take reference from following code example :

    <record id="account_move_comp_rule_group" model="ir.rule.group">
        <field name="name">Account Entry</field>
        <field ref="model_account_move" name="model_id"/>
        <field eval="True" name="global"/>
    </record>

    <record id="account_move_comp_rule" model="ir.rule">
        <field model="ir.model.fields" name="field_id" search="[('model','=','account.move'),('name','=','company_id')]"/>
        <field name="operator">child_of</field>
        <field name="operand">user.company_id.id</field>
  <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
        <field name="rule_group" ref="account_move_comp_rule_group"/>
    </record>

There so many similar example exist in tiny trunk code in security file.
You can check from :https://code.launchpad.net/~openerp/openobject-addons/trunk.

Hope this will help you.
Thanks.

Can you help with this problem?

Provide an answer of your own, or ask KAKA1.2 for more information if necessary.

To post a message you must log in.