2010-10-25 111 views
0

我正在構建一個小型應用程序,可以查看車輛的可用性並幫助客戶預訂它。Django更新多個查詢

在這我正在使用一個表單嚮導,引導用戶通過這些步驟。

在後端我正在更新所有查詢成功,但我不太確定如何執行它的一部分。

我有兩個型號,報價和車輛

車輛is_bok =真或假 報價看is_booked車輛=假

當用戶產生了他的報價,他可以看到有多少車輛可即5.

,如果我再選擇2輛,我想前兩個可用的車輛更新爲is_booked=True

### Check the vehicle availability and deduct amount of vehicles booked 
     amount_of_vehicles = 2 
     vehicle = Vehicle.objects.filter(is_booked=False) 
     ### run update for each vehicle 
     vehicle.update() 

我將如何實現這一目標?

回答

2

編輯。由於切片(限制),您必須針對查詢集中的每個元素運行更新查詢:

amount_of_vehicles = 2 
vehicles = Vehicle.objects.filter(is_booked=False)[0:amount_of_vehicles] 
for vehicle in vehicles: 
    vehicle.is_booked = True 
    vehicle.save() 
+0

聲明錯誤:切片已被佔用後無法更新查詢。 – ApPeL 2010-10-25 09:12:40

+0

@ApPeL更新! – Anpher 2010-10-25 09:21:07