0
我是新的蟒蛇,我正在制定一個系統,處理request.GET方法... 當我通過日期到系統中,如果存在,那麼它會根據該日期和其他日期查找數據庫並顯示數據,否則會顯示在這裏與其他日期的日期...... 是我的代碼:異常是:不支持的操作數類型爲 - :'str'和'datetime.timedelta'
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
我只需要2011-12-01
。我的問題是我怎麼能得到2011-12-01
是日期時間格式不是字符串? 任何人都可以提供一個關於我的情況的提示或想法? 謝謝