开发者

Django update multiple queries

I am building a small app that looks at availability of vehicles and helps a customer book it.

in this I am making use of a form wizard that guides the user through the steps.

in the backend I am updating all the queries successfully, however I am not quite sure on how to execute one part of it.

I have two models, Quote and Vehicle

Vehicle is_bok = True or False Quote looks at the vehicle is_booked=False

When the user generates his quote he can see how many vehicles are available ie, 5.

if I then choose 2 vehicles, I would like to update the first two available vehicles to 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()

How would I achieve this?


EDIT. You have to run update query for each element in queryset because of slice (limit):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜