根据days_to_add
,'Start_Date'
和'Cycle_Type'
所依据的'Cycle_val'
的数量,我必须显示日期,星期几
EX
如果
'Start_Date'
= 01/08/2017,
'Cycle_Type'
=天
'Cycle_val'
= 3
我应该有一棵树
日期------------------------------ ---- day
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>
&#13;
答案 0 :(得分: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字段。