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

Use a domain filter on many2one field

By
Emanuel Cino
on 3/14/14, 4:49 AM 9,241 views

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 !

1
Eric
On 3/14/14, 7:37 PM

Your church_id field sounds like it should be related to a church table, but I think you just want to indicate if a partner record is a church or not. I would just create a field called 'is_church' as a boolean, then you can use a domain to filter for any partner records where the is_church value is set to true like this

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

You can get rid of the church_id field because it's not related to a church table. I believe that should work for you. Hope it helps

in your .py file:

_columns{
    'is_church': fields.boolean('Is a Church', domain="[('is_church', '=', True)]")
}

You're right, I was doing it unnecessarily hard. That's far better so !

Emanuel Cino
on 3/17/14, 4:25 AM

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: 3/14/14, 4:49 AM
Seen: 9241 times
Last updated: 12/23/15, 5:38 PM