2017-08-07 108 views
1

根據它所依賴的'Start_Date''Cycle_Type''Cycle_val'我有顯示日期,星期幾如何添加動態字段?

EX

如果

'Start_Date' = 1月8日的days_to_add數/ 2017,

'Cycle_Type' =天

'Cycle_val' = 3

我768,16具有樹形

日期------------------------------ ----天

01/08/2017 --------------------------星期二

02/08/2017 --------------------------星期三

03/08/2017 -------------- ------------星期四

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')

編輯

<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>

回答

1

上創建另一個模型,其是用於樹的形式視圖按提供的數據顯示日期在窗體視圖中。

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

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

在你的模型中取這個模型的Many2many字段並用下面的代碼替換你的代碼。

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)]}} 

現在,將新的Many2many字段放置到窗體視圖中。

+0

感謝您的答案,但它仍然只顯示一個,請參閱編輯plz – Borealis

+0

我更新了我的答案。覈實。 –

+0

它顯示日期和日期,但現在不顯示最後一條記錄 – Borealis