Skip to Content
मेन्यू
This question has been flagged
2 Replies
990 Views

i am creating a one to many relation in child model where i have a many to one field .

here i want to make it readonly but it is not getting saved in database when i am creating a record

can anyone give me solution for this?

Avatar
Discard
Author Best Answer

no, its not getting saved in database .

I am adding default records in one to many where I have a many-to-one field which is readonly and it is not getting saved .



Avatar
Discard

Can i see the code u use

Author

class hr_skg_attendance ( models . Model ):

_name = 'hr.skg.attendance'

company_id = fields . Many2one ( 'res.company' , 'name' , copy = False , readonly = True , default = lambda self : self .env.company )

employee_list_name = fields . Char ( string = "Name" , required = True )

attendance_list_line_ids = fields . One2many ( 'hr.skg.attendance.lines' , 'attendance_id' , string = 'employee list' , store = True )

@api .​ model

def default_get ( self , vals ):

res = super ( hr_skg_attendance , self ). default_get ( vals )

_logger . info ( 'this is default_get function' )

attendance_list_line_ids = [( 5 , 0 , 0 )]

attendance_rec = self . env [ 'hr.employee' ].search([])

_logger . info ( attendance_rec )

for rec in attendance_rec :

line = ( 0 , 0 ,{ 'working_days':26,

​ ​ ​'skg_employee_id' : rec .id, ​

​ ​ 'skg_employee_code' : rec .employee_code,

​ ​'total_attendance':0, #'over_time_hrs':0 ,

​ 'Advance_amount':0,

' ​working_days':0, })

attendance_list_line_ids . append ( line )

res [ 'attendance_list_line_ids' ] = attendance_list_line_ids

return res

class hr_skg_attendance_lines(models.Model):

_name= 'hr.skg.attendance.lines'

_description='hr skg attendance lines'

working_days = fields.Float(string='Working Days',default=26,readonly=True)

skg_employee_id = fields.Many2one('hr.employee',string='Employee')

total_attendance = fields . Float ( string = 'Total Attendance' )

over_time_hrs = fields . Float ( string = 'Over Time(Hr)' )

Advance_amount = fields . Float ( string = 'Advance amount' )

loan_emi = fields . Float ( string = 'Loan EMI' ) attendance_id = fields . Many2one ( 'hr.skg.attendance' , string = 'attendance' )

skg_employee_code = fields . Integer ( related = 'skg_employee_id.employee_code' , string = "Emp code" )

xml code-

< notebook >

< page string = "attendance" name = 'attendance' >

< field name = "attendance_list_line_ids" >

< tree string = "Employees attendance" editable = 'bottom' create = "0" >

< field name = "working_days" width = "200px" />

< field name = "skg_employee_id" width = "200px" readonly = "1" options = "{'no_open': True}" />

​ ​ < field name = "skg_employee_code" width = "200px" />

​ ​ < field name = "total_attendance" width = " 200px" />

​ ​ < field name = "over_time_hrs" width = " 200px " / >

​ ​ < field name = "Advance_amount" width = "200px" />

​ ​ < field name = "loan_emi" width = "200px" />

</ tree >

</ field >

</ page >

</notebook>

Author

class hr_skg_attendance ( models . Model ):

_name = 'hr.skg.attendance'

company_id = fields . Many2one ( 'res.company' , 'name' , copy = False , readonly = True , default = lambda self : self .env.company )

employee_list_name = fields . Char ( string = "Name" , required = True )

attendance_list_line_ids = fields . One2many ( 'hr.skg.attendance.lines' , 'attendance_id' , string = 'employee list' , store = True )

@api .​ model

def default_get ( self , vals ):

res = super ( hr_skg_attendance , self ). default_get ( vals )

_logger . info ( 'this is default_get function' )

attendance_list_line_ids = [( 5 , 0 , 0 )]

attendance_rec = self . env [ 'hr.employee' ].search([])

_logger . info ( attendance_rec )

for rec in attendance_rec :

line = ( 0 , 0 ,{ 'working_days':26,

​ ​ ​'skg_employee_id' : rec .id, ​

​ ​ 'skg_employee_code' : rec .employee_code,

​ ​'total_attendance':0, #'over_time_hrs':0 ,

​ 'Advance_amount':0,

' ​working_days':0, })

attendance_list_line_ids . append ( line )

res [ 'attendance_list_line_ids' ] = attendance_list_line_ids

return return

class hr_skg_attendance_lines(models.Model):

_name= 'hr.skg.attendance.lines'

_description='hr skg attendance lines'

working_days = fields.Float(string='Working Days',default=26,readonly=True)

skg_employee_id = fields.Many2one('hr.employee',string='Employee')

total_attendance = fields . Float ( string = 'Total Attendance' )

over_time_hrs = fields . Float ( string = 'Over Time(Hr)' )

Advance_amount = fields . Float ( string = 'Advance amount' )

loan_emi = fields . Float ( string = 'Loan EMI' )
attendance_id = fields . Many2one ( 'hr.skg.attendance' , string = 'attendance' )

skg_employee_code = fields . Integer ( related = 'skg_employee_id.employee_code' , string = "Emp code" )

xml code-

< notebook >

< page string = "attendance" name = 'attendance' >

< field name = "attendance_list_line_ids" >

< tree string = "Employees attendance" editable = 'bottom' create = "0" >

< field name = "working_days" width = "200px" />

< field name = "skg_employee_id" width = "200px" readonly = "1" options = "{'no_open': True}" />

​ ​ < field name = "skg_employee_code" width = "200px" />

​ ​ < field name = "total_attendance" width = " 200px" />

​ ​ < field name = "over_time_hrs" width = " 200px " / >

​ ​ < field name = "Advance_amount" width = "200px" />

​ ​ < field name = "loan_emi" width = "200px" />

</ tree >

</ field >

</ page >

</notebook>

Author

crated attendance_id and added store=true to one to many but still not getting saved in database.

Author

only the skg_employee_id and skg_empoloyee_code(related fieldd) is not getting saved in database.

Best Answer

Try to add store=true to ur One2many fields.

u have to create a many2one field  "attendance_id" in the child model ....lines

Avatar
Discard