I want to calculate days difference between two dates in Django. i am trying this code:
我想计算Django中两个日期之间的天差。我正在尝试这段代码:
My Model :
我的型号:
dateDeDebut = models.DateTimeField(auto_now_add=False, auto_now=False)
dateDeFin = models.DateTimeField(auto_now_add=False, auto_now=False)
My classViewSet :
我的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)
And I get this error :
我收到这个错误:
unsupported operand type(s) for -: 'unicode' and 'unicode'
1 个解决方案
#1
0
This solution works for me
这个解决方案适合我
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)
#1
0
This solution works for me
这个解决方案适合我
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)