from datetime import datetime from netforce.model import Model, fields class PeriodLine(Model): _name="clinic.period.line" _string="Period Line" def _get_total(self,ids,context={}): res={} fmt='%Y-%m-%d' for obj in self.browse(ids): start=datetime.strptime(obj.date_start,fmt) stop=datetime.strptime(obj.date_stop,fmt) days=(stop-start).days res[obj.id]=days return res def _get_name(self,ids,context={}): res={} for obj in self.browse(ids): res[obj.id]='%s - %s'%(obj.date_start, obj.date_stop) return res _fields={ 'name': fields.Char("Name",function="_get_name",store=True), "period_id": fields.Many2One("clinic.period", "Period"), 'date_start': fields.Date("Date Start"), 'date_stop': fields.Date("Date Stop"), 'day_total': fields.Integer("Duration (Day)",function="_get_total"), 'state': fields.Selection([['draft','Draft'],['done','Done']],"State"), 'close': fields.Boolean("Close"), } _defaults={ 'state': 'draft', 'close': False, } def create(self,vals,**kw): id=super().create(vals,**kw) self.function_store([id]) return id def write(self,ids,vals,**kw): super().write(ids,vals,**kw) self.function_store(ids) PeriodLine.register()