我想计算Django中两个日期之间的天差。我正在尝试这段代码:
我的模特:
dateDeDebut = models.DateTimeField(auto_now_add=False, auto_now=False)
dateDeFin = models.DateTimeField(auto_now_add=False, auto_now=False)
我的classViewSet:
class MyClassViewSet(viewsets.ModelViewSet):
serializer_class = MyClassSerializer
permission_classes = (IsAuthenticated, )
def perform_create(self, serializer):
if (condition):
diff = (self.request.data.get('dateDeFin') - self.request.data.get('dateDeDebut')).days #never executed
print(diff)
我收到了这个错误:
unsupported operand type(s) for -: 'unicode' and 'unicode'
答案 0 :(得分:0)
此解决方案适合我
class MyClassViewSet(viewsets.ModelViewSet):
serializer_class = MyClassSerializer
permission_classes = (IsAuthenticated, )
def perform_create(self, serializer):
if (condition):
start_date = datetime.datetime.strptime(self.request.data.get('dateDeFin'), "%Y-%m-%d %H:%M")
end_date = datetime.datetime.strptime(self.request.data.get('dateDeDebut'), "%Y-%m-%d %H:%M")
diff = abs((end_date-start_date).days)
print(diff)