I was trying to update a employee profile(fill up a one2many tree list in employee profile) like the one in the following image
But this is based on the following form,
Now the above form is of class 'asset.asset' and has one many2one**(Assigned To)** field to 'hr.employee' class. When I click save, particular asset name and few details had to be updated in the one2many tree view of first image.
Following is the modification of code:
asset_asset.py
class asset_asset(osv.osv):
"""
Assets
"""
_name = 'asset.asset'
_description = 'Asset'
_inherit = ['mail.thread']
CRITICALITY_SELECTION = [
('0', 'General'),
('1', 'Important'),
('2', 'Very important'),
('3', 'Critical')
]
_columns = {
'name': fields.char('Asset Name', size=64, required=True, translate=True),
'finance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','0')]),
'warehouse_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','1')]),
'manufacture_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','2')]),
'maintenance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','3')]),
'maintenance_state_color': fields.related('maintenance_state_id', 'state_color', type="selection", selection=STATE_COLOR_SELECTION, string="Color", readonly=True),
'criticality': fields.selection(CRITICALITY_SELECTION, 'Criticality'),
'property_stock_asset': fields.property(
type='many2one',
relation='stock.location',
string="Asset Location",
store=True,
help="This location will be used as the destination location for installed parts during asset life."),
'user_id': fields.many2one('hr.employee', 'Assigned to', track_visibility='onchange'),
'employee_id': fields.many2one('hr.employee', 'Ref'),
'active': fields.boolean('Active'),
'profit_id':fields.char('Profit Center', size=64, readonly=True),
'issue_date':fields.date('Issued On'),
'return_date':fields.date('Returned On'),
'test':fields.char('Employee'),
'asset_number': fields.char('Asset Number', size=64),
'model': fields.char('Model', size=64),
'serial': fields.char('Serial no.', size=64),
'vendor_id':fields.many2one('res.partner', 'Vendor'),
'manufacturer_id': fields.many2one('res.partner', 'Manufacturer'),
'start_date': fields.date('Start Date'),
'purchase_date': fields.date('Purchase Date'),
'warranty_start_date': fields.date('Warranty Start'),
'warranty_end_date': fields.date('Warranty End'),
'maintain_asset': fields.selection([
('month', 'Monthly'),
('qtr', 'Quarterly'),
('half', 'Half Yearly'),
('year', 'Yearly'),
], 'Maintenance Duration', readonly= False, select=True),
# image: all image fields are base64 encoded and PIL-supported
'image': fields.binary("Image",
help="This field holds the image used as image for the asset, limited to 1024x1024px."),
'image_medium': fields.function(_get_image, fnct_inv=_set_image,
string="Medium-sized image", type="binary", multi="_get_image",
store={
'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Medium-sized image of the asset. It is automatically "\
"resized as a 128x128px image, with aspect ratio preserved, "\
"only when the image exceeds one of those sizes. Use this field in form views or some kanban views."),
'image_small': fields.function(_get_image, fnct_inv=_set_image,
string="Small-sized image", type="binary", multi="_get_image",
store={
'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Small-sized image of the asset. It is automatically "\
"resized as a 64x64px image, with aspect ratio preserved. "\
"Use this field anywhere a small image is required."),
}
def _employee_get_asset(self, cr, uid,ids, context=None):
for rec in self.browse(cr,uid,ids,context=context):
if user_id:
employee = self.pool.get('hr.employee').browse(cr, uid, user_id, context=context)
proj_obj = self.pool.get('hr.employee.asset')
values = {'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date}
proj_obj.write(cr, user, proj_ids, values)
proj_obj.create(cr,uid,{'emp_id':rec.user_id, 'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date},context=context)
return True
_defaults = {
'active': True,
'employee_id': _employee_get_asset,
}
Class for one2many tree view in Employee profile
class hr_employee_asset(osv.osv):
_name = "hr.employee.asset"
_columns = {
'name': fields.char('Asset Name', size=64),
'issue_date':fields.date('Issued On'),
'return_date':fields.date('Returned On'),
'emp_id':fields.many2one('hr.employee','Employee ID'),
}
Added a one2many field in hr.employee
class hr_employee(osv.osv):
_inherit="hr.employee"
_columns={
'asset_hr':fields.one2many('asset.asset','employee_id','Employee Assets'),
'asset_ed':fields.one2many('hr.employee.asset','emp_id','Employee ID'),
}
hr_employee()
Someone please suggest me the right way of doing this as I am totally messed up on this scenario. Any guidance would be really valuable.