在我的模块中,我有以下many2one字段:
'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')
其中xx.insurance.type
如下:
class InsuranceType(osv.Model):
_name='xx.insurance.type'
_columns = {
'name' : fields.char(size=128, string = 'Name'),
'sale_ids': fields.one2many('sale.order', 'xx_insurance_type', string = 'Sale orders'),
'insurance_percentage' : fields.float('Insurance cost in %')
}
我知道many2one字段将 name 字段作为其显示名称,但我希望它以name
和insurance_percentage
的形式使用name + " - " + insurance_percentage + "%"
和get_name
的组合。 {1}}
我认为最好覆盖def get_name(self,cr, uid, ids, context=None):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = []
for record in self.browse(cr, uid, ids, context=context):
name = record.name
percentage = record.insurance_percentage
res.append(record.id, name + " - " + percentage + "%")
return res
方法,所以我尝试了以下内容:
function AddToBag()
{
var url = "/Home/Populate_Entitle/";
$.ajax({
url: url,
data: { id: 1 },
cache: false,
type: "POST",
success: function (data) {
alert("hi");
$("#gridContent").html(markup);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
并将其放在ÌnsuranceType`类中。 什么都没发生: 我必须将它放在包含该字段的主类中吗?如果是这样,还有其他方法可以做到这一点,因为这可能也会改变其他many2one字段的显示方式吗?
答案 0 :(得分:12)
如果您不想更改与模型many2one
相关的其余xx.insurance.type
的显示名称,则可以在XML视图中将上下文添加到many2one
要修改其显示名称:
<field name="xx_insurance_type" context="{'special_display_name': True}"/>
然后,在name_get
函数中:
def name_get(self, cr, uid, ids, context=None):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
res = []
if context.get('special_display_name', False):
for record in self.browse(cr, uid, ids, context=context):
name = record.name
percentage = record.insurance_percentage
res.append(record.id, name + " - " + percentage + "%")
else:
# Do a for and set here the standard display name, for example if the standard display name were name, you should do the next for
for record in self.browse(cr, uid, ids, context=context):
res.append(record.id, record.name)
return res
答案 1 :(得分:0)
@api.depends('name', 'insurance_percentage')
def name_get(self):
res = []
for record in self:
name = record.name
if record.insurance_percentage:
name = '[' + record.insurance_percentage+ ']' + name
res.append((record.id, name))
return res