简体   繁体   中英

Openerp how to update field values in two classes

There are two classes that I am currently working with and I am in a mess right now. Please help me to solve this

What I need to do is once an employee in "roster_allocation" class want to cancel his/her allocated roster, then for that purpose you have to go through "roster_substitution"class, which keeps records of the original allocation and substitute allocation and date and time. Once you entered a record via that table I want to update that particular "roster_allocation" table record with updating the existing employee_id with the substitute_employee_id from **"roster_substitute"**table.

these are my two classes

Roster Allocation Class

   class roster_allocation(osv.osv):



         _name="roster.allocation"
         _description ="Allocate rosters on employees"

         _columns={

             'emp_id':fields.many2one('hr.employee','Employee'),
             'department_id':fields.many2one('hr.department','Department'),
             'roster_allocation_allocation':fields.one2many('roster.days.allocation','roster_allocation_connection','Days connection'),
             'roster_time':fields.char('Take the related field  roster_time.name'),
             'monthly allocation':fields.char('Month')   ,
             'roster_rest_allocation':fields.one2many('roster.rest.days','roster_id','Rest Days'),
             'roster_substitute':fields.one2many('roster.substitution','allocation_id','Substitution'),

roster_allocation()

Roster days allocation class ( which keeps the records of start date, roster type and time slot)

class roster_days_allocation(osv.osv):
    _name="roster.days.allocation"
    _description = "To allocate days in to the already defined time slots"


    def get_domain_useer_id(self,cr,uid,ids,roster_type_list,context=None):
      mach=[]
      filter = self.pool.get('roster.type').search(cr,uid,[('id','=',roster_type_list)])
      return {'domain':{'roster_time_list':[('rostertype','=',filter)]}}


    _columns={
            'name':fields.char('Days_Allocation'),  
            'allocation_start_day':fields.date('Start Date',required=True),
            'allocation_end_day':fields.date('End Date',required=True),
            'type_connection':fields.one2many('roster.type','date_allocation_connection','Roster Type'),
            'roster_type_list':fields.many2one('roster.type','Rosters'),
            'roster_time_list':fields.many2one('roster.time','Time Slot'),

            'roster_allocation_connection':fields.many2one('roster.allocation','Allocation Connection')


              }


roster_days_allocation()

Roster Substitution class (which keeps the record of originally allocated employee and substitute employee)

class roster_substitution(osv.osv):



    _name="roster.substitution"
    _description="Substituting employees "
    _columns={
             'allocation_id':fields.many2one('roster.allocation','Allocation'),
             'employee':fields.many2one('hr.employee','Employee'),
             'sub_employee':fields.many2one('hr.employee','Employee'),
             'time_slot':fields.many2one('roster.time','Roster'),
             'roster_day':fields.date('Day'),
             'reason':fields.text('Reason'),
             'department_id':fields.many2one('hr.department','Department'),


             }


    def onchange_date(self, cr, uid, ids, roster_date):
        result = {'value': {'type': False}}
        if type_id:
            type = self.pool.get('roster.time').browse(cr, uid, roster_date)
        result['value'] = {'time_slot': type.name.id}
        return result



roster_substitution()

Please help me to solve this

I'm sorry I read your post over and over but I don't understand what you are exactly trying to do (might be because you name some fields different ways in the code and in the text). But anyway:

I think you have to modify your emp_id in your roster_allocation class.

'emp_id': fields.function(_creation_function,
                                     type="many2one",
                                     obj='hr.employee',
                                     string='Employee', 
                                     store={
                                     'roster.substitution': (_modify_function,
                                                            ['field_that_change'], 
                                                            10)}),

with _creation_function the function you have to define for the value emp_id has when you create it. _modify_function the function that will modify your emp_id when field_that_change will be modified in 'roster.substitution'.

This is because onchange don't allow you to modify a field in model_A from model_B. It doesn't save anything in the database except if the field that is updated thanks to the onchange is modified on your screen and you save afterward.

If you want to add some fields you want to keep an eyes on you can add them in ['field_that_change', 'second_field'] if in the same object, or by adding a second objet with the same syntax in the store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM