Borealis
Borealis

Reputation: 1137

how can I add dynamic fields?

According to the number of days_to_add that it depends on 'Start_Date', 'Cycle_Type' and 'Cycle_val' I have to display date, day of the week

EX

if

'Start_Date'=01/08/2017,

'Cycle_Type'=day

'Cycle_val'=3

I shoud have a tree

date ------------------------------ ---- day

01/08/2017-------------------------- tuesday

02/08/2017-------------------------- wednesday

03/08/2017-------------------------- thursday

class Shift(models.Model):
    _name = "working_shift"
    _description = "Working Shift"

    name = fields.Char(required=True, string ='Name of the shift')
    Start_Date = fields.Datetime(string='Start date', required=True, index=True,default=lambda s: fields.Datetime.now())
    Cycle_Type=fields.Selection([('week','week'),('day','day')],readonly=False,default='week')
    Cycle_val = fields.Integer(required=True, index=True,readonly=False )
    Stop_Date = fields.Datetime(string='Stop date', required=True, index=True,default='')
    Stop_Date_day = fields.Char(string='Stop date day', required=True, index=True,default='')
    Start_Date_day = fields.Char(string='Start date day', required=True, index=True,default='')
    @api.onchange('Start_Date', 'Cycle_Type', 'Cycle_val')
    def _compute_Stop_Date(self):
       
        for record in self:
            days_to_add1=0
            days_to_add=0
            current_date = fields.Datetime.from_string(record.Start_Date)
            day_on=fields.Datetime.from_string(record.Start_Date)
            if record.Cycle_Type == 'day':
                days_to_add=record.Cycle_val           
                while days_to_add > 1:
                    current_date += timedelta(days=1) 
                    days_to_add -= 1

            else:
                days_to_add1=record.Cycle_val
                days_to_add=days_to_add1*7           
                while days_to_add > 1:
                    current_date += timedelta(days=1) 
                    days_to_add -= 1                
                    
            record.Stop_Date=current_date
            record.Stop_Date_day=current_date.strftime('%A')
            record.Start_Date_day=day_on.strftime('%A')

edits

<notebook>


                    <page name="working" string="working time">
                        <group>
                            <field name="Date_range" mode="tree,kanban" > 
                                    <tree string="workings">
                                           
                                           <field name="date"/>
                                            <field name="day"/>
                                           
                                    </tree>
                           </field>
                         </group>
                    </page>
</notebook> 

Upvotes: 1

Views: 201

Answers (1)

Chavada Viki
Chavada Viki

Reputation: 1524

Create on another model which is for the tree to display dates in the form view as per the data provided in the form view.

class Shift_date_range(models.Model):
    _name = "working.shift.date.range"
    _description = "Working Shift Date Range"

    date = fields.Date('Date')
    day = fields.Char('Day')

Take Many2many field of this model in your model and replace your code with below.

class Shift(models.Model):
    _name = "working_shift"
    _description = "Working Shift"

    name = fields.Char(required=True, string ='Name of the shift')
    Start_Date = fields.Datetime(string='Start date', required=True, index=True,default=lambda s: fields.Datetime.now())
    Cycle_Type=fields.Selection([('week','week'),('day','day')],readonly=False,default='week')
    Cycle_val = fields.Integer(required=True, index=True,readonly=False )
    Stop_Date = fields.Datetime(string='Stop date', required=True, index=True,default='')
    Stop_Date_day = fields.Char(string='Stop date day', required=True, index=True,default='')
    Start_Date_day = fields.Char(string='Start date day', required=True, index=True,default='')
    Date_range = fields.Many2many('working.shift.date.range',string="Date Range")

    @api.onchange('Start_Date', 'Cycle_Type', 'Cycle_val')
    def _compute_Stop_Date(self):
        date_range_obj= self.env['working.shift.date.range']
        days_to_add1=0
        days_to_add=0
        list_date_range_ids = []
        current_date = fields.Datetime.from_string(self.Start_Date)
        day_on=fields.Datetime.from_string(self.Start_Date)
        if self.Cycle_Type == 'day':
            days_to_add=self.Cycle_val           
            while days_to_add >= 1:
                date_range_id= date_range_obj.create({'date':current_date,'day':current_date.strftime('%A')})
                list_date_range_ids.append(date_range_id.id)
                current_date += timedelta(days=1) 
                days_to_add -= 1

        else:
            days_to_add1=self.Cycle_val
            days_to_add=days_to_add1*7           
            while days_to_add >= 1:
                date_range_id= date_range_obj.create({'date':current_date,'day':current_date.strftime('%A')})
                list_date_range_ids.append(date_range_id.id)
                current_date += timedelta(days=1) 
                days_to_add -= 1                

        self.Stop_Date=current_date
        self.Stop_Date_day=current_date.strftime('%A')
        self.Start_Date_day=day_on.strftime('%A')
        return {'value':{'Date_range':[(6,0,list_date_range_ids)]}}

Now, place that new Many2many field of the form view.

Upvotes: 1

Related Questions