OpenERP-使用外部ID比较字段? [英] OpenERP - Compare fields using external id?
问题描述
是否可以使用外部ID(而非内部ID)比较视图中的字段.我需要这个,因为我需要根据其他字段隐藏一些字段.我可以使用内部数据库ID来完成这项工作.因此,如果用户选择国家(例如,检查该国家/地区的ID),则如果ID相匹配(如果比较),则会显示另一个字段.例如这样的
Is it possible to compare fields in views using external id (not internal one). I need this because I need to hide some fields depending on another fields. I can make this work using internal database id. So if user chooses for example country (in view it checks that countries id), if id matches in view that it is compared, it shows another field. For example like this:
<field name="some_field"
attrs="{'invisible': [('country_id','!=',10)]}"
/>
这是可行的,但是它并不是真正可靠的.试想一下,如果ID会发生变化(例如安装我的模块ID将已经采取了一些其他国家),那么它会显示
This one works, but it is not really reliable. Imagine if id would change (for example installing my module that id would be already taken by some other country), then it would show some_field
when different country would be chosen. And that is not intended.
So I thought about using external id, which you provide when adding data into tables in xml files. That id is static and actually should be the same for any database you would install that module (because that id is provided in module itself, not created by database automatically).
Is there a way to use this same feature, but using external id?
Of course maybe someone knows better approach to solve such problem?
P.S.不能选择添加另一个字段,如boolean来显示hide some_field
,因为它完全取决于将在模块安装时添加的特定值.
P.S. adding another field like boolean to show hide some_field
is not an option, because it depends entirely on specific values that would be added on module install.
推荐答案
您可以使用fields_view_get添加此功能.例如
You can use fields_view_get to add this functionality. For example
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
if context is None:context = {}
res = super(your_osv_class_name, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
doc = etree.XML(res['arch'])
nodes = doc.xpath("//field[@name='some_field']")
id_found = self.pool.get('res.country').search(cr, uid,[('name','=','India')])[0]
#or try to search for the id using the external id
for node in nodes:
node.set('attrs', "{'invisible':[('country_id','=',%s)]}"%id_found)
setup_modifiers(node, res['fields']['some_field'])
res['arch'] = etree.tostring(doc)
return res
这篇关于OpenERP-使用外部ID比较字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!