How do I calculate number of days between two dates stored in a database?
我正在尝试创建一个模块,它从数据库中获取两个不同的日期,创建日期和赢得日期,计算两个日期之间的天数,并将结果存储在一个新字段"比较日期"中。
这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def action_set_won(self, cr, uid, ids, context=None): """ Won semantic: probability = 100 (active untouched)""" stages_leads = {} for lead in self.browse(cr, uid, ids, context=context): stage_id = self.stage_find(cr, uid, [lead], lead.team_id.id or False, [( 'probability', '=', 100.0), ('on_change', '=', True)], context=context) if stage_id: if stages_leads.get(stage_id): stages_leads[stage_id].append(lead.id) else: stages_leads[stage_id] = [lead.id] for stage_id, lead_ids in stages_leads.items(): self.write(cr, uid, lead_ids, {'stage_id': stage_id}, context=context) return self.write(cr, uid, ids, {'probability': 100, 'won_date': datetime.now(), 'compare_dates': abs('won_date' - 'create_date')}, context=context) |
奥多说:
1 2 | File"/mnt/extra-addons/crm_updates/models/models.py", line 37, in action_set_won return self.write(cr, uid, ids, {'probability': 100, 'won_date': datetime.now(), 'compare_dates': abs(('won_date') - ('create_date'))}, context=context) |
号
类型错误:不支持-:"str"和"str"的操作数类型
我知道该怎么做。
下面是一个相当简单的解决方案:
1 2 3 | # You need the datetime package to calculate between dates from datetime import datetime compare_dates = abs((won_date - create_date).days) |
此处提供更多信息