[英]OpenERP How to use a domain filter on many2one field?
I am new to OpenERP (v7) and am writing a module that extends the res.partner class and added the following two fields : 我是OpenERP(v7)的新手,正在编写一个扩展res.partner类并添加以下两个字段的模块:
_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. 我想做的是,当用户打开church_id视图时,它仅显示作为教会的伙伴。 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.
教堂是具有与教堂类别相对应的类别ID(res.partner.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 :
以下函数正确返回包含当前用户ID和教堂ID列表的字典:
# 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 : 我在view church_id上尝试了以下域,并且总是收到相同的错误:
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 !
我也尝试过在相关领域做它,但是我不知道如何实现它。非常感谢您的帮助!
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. 有人建议我,church_id字段应与教堂表相关,但我只想指出伙伴记录是否为教堂。 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
因此,我可以将一个名为“ is_church”的字段创建为布尔值,然后使用域来过滤其中is_church值设置为true的任何伙伴记录
domain = "[('is_church','=',True)]"
I can get rid of the church_id field because it's not related to a church table. 我可以摆脱church_id字段,因为它与教堂餐桌无关。
In the .py file: 在.py文件中:
_columns{
'is_church': fields.boolean('Is a Church', domain="[('is_church', '=', True)]")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.