Question

I am new to OpenERP (v7) and am writing a module that extends the res.partner class and added the following two fields :

_columns = {
    'member_ids': fields.one2many('res.partner', 'church_id', 'Members', domain=[('active','=',True)]),
    'church_id': fields.many2one('res.partner', 'Church', domain="[('is_company','=',True)]")
}

What I would like to do, is that when the user opens the church_id view, it shows only partners that are churches. For now, it displays all companies as I am not able to set correctly the domain. A church is a company that has the category id (res.partner.category) corresponding to the church category. I tried to solve my problem using a functional field, but I am only getting some errors. The following function returns correctly a dictionary containing the current user id and the list of the church ids :

# Returns : {'user_id': [church_id1, church_id2, ...]}
def _get_church_ids(self, cr, uid, ids, field_name, arg, context=None):
    sql_req = """
        SELECT R.partner_id 
        FROM res_partner_res_partner_category_rel R
            LEFT JOIN res_partner_category C ON ( R.category_id = C.id)
        WHERE C.active = TRUE AND UPPER(C.name) = 'CHURCH'
    """
    cr.execute(sql_req)
    sql_res = cr.fetchall()
    return dict.fromkeys(ids, sql_res)

Corresponding field and view :

'church_ids': fields.function(_get_church_ids, type="one2many", relation='res.partner', method=True)
<field name="church_ids" invisible="1"/>

I tried the following domains on the view church_id, and I always get the same error :

Uncaught Error: Expected "]", got "(name)"

<field name="church_id" attrs="{'invisible': [('is_company','=',True)]} domain="[('id','in',[church for church in church_ids[id]])]"/>
<field name="church_id" attrs="{'invisible': [('is_company','=',True)]} domain="[('id','in',[church[0] for church in church_ids)]"/>

Any suggestions on how to do this ? I spent already some days trying to figure it out but with no luck. I also tried to do it with a related field but I couldn't understand how to achieve it... Many thanks for your help !

Was it helpful?

Solution

Someone suggested me that the church_id field should be related to a church table, but I just wanted to indicate if a partner record is a church or not. Therefore, I could just create a field called 'is_church' as a boolean, then use a domain to filter for any partner records where the is_church value is set to true like this

domain = "[('is_church','=',True)]"

I can get rid of the church_id field because it's not related to a church table.

In the .py file:

_columns{
    'is_church': fields.boolean('Is a Church', domain="[('is_church', '=', True)]")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top