Related type field not showing value

2019-04-16 13:07发布

问题:

I have a related field 'region_id' that get the value from a many2one field in another class like this

 class activity_summary(osv.osv):
_name = "budget.activity_summary"
_rec_name = "activity_summarycode"
_columns = {
'activity_summarycode' : fields.many2one("budget.activity_year", "Activity Summary Code", ondelete= "no action", required=True ),
    'region_id' : fields.related("activity_summarycode", "mgmt_code", type="char", string = "Management Code", size=64, store = True),
}   

But it's not giving the right value, instead when i save, it gives me this: browse_record(budget.org_table, 12). Why is it not returning the value? and how may i fix it?

Activity Year class

class activity_year(osv.osv):
_name = "budget.activity_year"
_description = "Activity year"
_rec_name = "activity_yearcode"
_columns = {
    'activity_yearcode' : fields.char("Activity Code", size=64, required=True),
'activity_name' : fields.char("Activity Name", size=128),
'mgmt_code' : fields.many2one("budget.org_table","Management Unit Code"),
    }
_sql_constraints = [
    ('activity_yearcode_unique', 'UNIQUE(activity_yearcode)', 'Each activity code is unique.'),
]

budget.org-table class

class org_table(osv.osv):
_name = "budget.org_table"
_rec_name = "org_code"
_columns = {
    'org_code' : fields.char("Code", size=64),
    'org_name' : fields.char("Name"),
  }
_sql_constraints = [
    ('org_code_unique', 'UNIQUE(org_code)', 'Each org_table ID is unique.'),
]

回答1:

In OpenERP, When you use related field, it's give value or output based on it's type. For example type='char', See other fields type.

In your case you need to use type='many2one' for getting desire output.

try this,

class activity_summary(osv.osv):
    _name = "budget.activity_summary"
    _rec_name = "activity_summarycode"
    _columns = {
        'activity_summarycode' : fields.many2one("budget.activity_year", "Activity Summary Code", ondelete= "no action", required=True ),
        'region_id' : fields.related("activity_summarycode", "mgmt_code", type="many2one", string = "Management Code", relation="budget.activity_year", size=64, store = True),
}