【发布时间】:2025-12-12 16:45:01
【问题描述】:
您好,我在 partner_view 的自定义 many2one 字段中覆盖 name_get,以根据上下文显示与 name 不同的字段
class partner_relation_role(osv.osv):
_name = "partner.relation.role"
_description = 'Types of relationships between contacts'
_columns = {
'name': fields.char('Nome', size=128, required=True),
'description': fields.text('Description'),
'name_parent': fields.char('Parent name'),
'name_child': fields.char('Child name'),
}
def name_get(self, cr, uid, ids, context=None):
if context is None:
context = {}
res = []
ret_name = ""
for object in self.browse(cr, uid, ids, context = context):
if object.name:
if context.get('parent_relation') is None:
ret_name = object.name or ""
elif context.get('parent_relation') is True:
# returns relation name for parent relation
ret_name = object.name_parent or object.name
elif context.get('parent_relation') is False:
ret_name = object.name_child or object.name
res.append((object.id,ret_name))
return res
并且在 res.partner 继承视图中:
<xpath expr="//page[@string='Contacts']" position="after">
<page name="uprelations" string="Relationships">
<field name="uprelation_ids" nolabel="1" context="{'default_partner_id': active_id}">
<tree editable="bottom">
<field name="related_partner_id" options="{'no_create': True}"/>
<field name="role_id" options="{'no_create': True}" context="{'parent_relation' : True}"/>
<field name="influence"/>
</tree>
</field>
</page>
<page name="downrelations" string="Has relations from">
<field name="downrelation_ids" nolabel="1" context="{'default_related_partner_id': active_id}">
<tree editable="bottom">
<field name="partner_id" options="{'no_create': True}"/>
<field name="role_id" options="{'no_create': True}" context="{'parent_relation' : False}" />
<field name="influence"/>
</tree>
</field>
</page>
</xpath>
这在我编辑记录时有效。当我保存它时,显示的是“名称”,而不是其他字段值。
更新 根据有帮助的 Adrian Merrall 的说法,在视图中使用它时,我应该将上下文放在视图动作中:但在这种情况下,相同的动作会打开一个具有不同上下文的两个页面的视图(第 1 页,上下文变量 = TRUE,第 2 页具有相同的变量 = FALSE)。 我应该把这个上下文放在哪里?
更新2 我重新思考整个逻辑。我应该为每个页面使用不同的 fields.related 吗? 目的是关联联系人并显示“与”不同的关系(上行关系:A 是 B 的代理人,下行关系:B 由 A 代表;A 有 C 的顾问,C 是 A 的顾问)。每个关系都有两个对等点和两个对等点:向上关系开始的对等点是“主人”或“父母”,另一个是“奴隶”或“孩子”。 同一个联系人可以是某些关系的主人,而另一些关系的奴隶。
【问题讨论】: