例外情况是:不支持的操作数类型 - :'str'和'datetime.timedelta'

时间:2011-12-03 06:33:02

标签: python django django-piston

我是python的新手,我正在建立一个处理request.GET方法的系统...... 当我将日期传递给系统时,如果它存在,那么它将查找数据库并根据该日期和其他日期显示数据,否则它将显示其他日期的日期... 这是我的代码:

from django.utils.xmlutils import SimplerXMLGenerator
from piston.handler import BaseHandler
from booking.models import *
from django.db.models import *
from piston.utils import rc, require_mime, require_extended, validate
import datetime

class BookingHandler(BaseHandler):
    allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
    fields = ('id', 'date_select', 'product_name', 'quantity', 'price','totalcost', 'first_name', 'last_name', 'contact', 'product')
    model = Booking
    #for product availability
    def read(self, request, id, date_select):
        if not self.has_model():
            return rc.NOT_IMPLEMENTED
        try:   
            prod = Product.objects.get(id=id)
            merge = []
            checkDateExist = Booking.objects.filter(date_select=date_select)
            if checkDateExist.exists():
                entered_date = Booking.objects.values('date_select').distinct('date_select').filter(date_select=date_select)[0]['date_select']
            else:             
                enteredTemp_date = datetime.datetime.strptime(date_select, '%Y-%m-%d')
                entered_date = datetime.datetime.strftime(enteredTemp_date,'%Y-%m-%d')


            delta = datetime.timedelta(days=3)
            target_date = entered_date - delta
            day = 1
            for x in range(0,5):
                delta = datetime.timedelta(days=x+day)
                new_date = target_date + delta
                maximumProdQuantity = prod.quantity
                quantityReserve = Booking.objects.filter(date_select=new_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
                if quantityReserve == None:
                    quantityReserve = 0

                data1 = {'maximum_guest': maximumProdQuantity, 'avialable': quantityReserve, 'date': new_date}
                merge.append(data1)    

            return merge       
        except self.model.DoesNotExist:
            return rc.NOT_HERE 

我收到了这个错误:

Piston/0.3dev (Django 1.3.1) crash report:

Method signature does not match.

Signature should be: id, date_select

Exception was: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

我认为在else语句中entered_date成为字符串,我执行此行entered_date = datetime.datetime.strftime(enteredTemp_date,'%Y-%m-%d'),以便日期格式不是2011-12-01 00:00:00我只需要{ {1}}。我的问题是如何得到2011-12-01日期时间格式而不是字符串? 谁能对我的案子提出暗示或想法? 感谢

2 个答案:

答案 0 :(得分:3)

strftime返回格式化字符串。如果要丢弃datetime对象的时间部分,请使用datetime.date

entered_date = datetime.datetime.strptime(date_select, '%Y-%m-%d')
entered_date = entered_date.date()

答案 1 :(得分:0)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''hi, your need to transfor the stirng to the date formation.
use this :
'''


import datetime
import time
from datetime import timedelta

today = datetime.date.today()
print(today)


# 0r 
from datetime import date
today1 = date.today()
print(today1)


tomorrow = today - datetime.timedelta(days=7)
print(str(tomorrow))

# it is ok, because the today is date formation


#but the following it is not ok

event_day= "2021-05-18"
tomorrow = event_day - datetime.timedelta(days=7)

# you should do this:
text='2021-5-10'
x=datetime.datetime.strptime(text, '%Y-%m-%d').date()

print(x) # it's ok


# hope this help you!