Django - using F () expression, but getting non-atomic update

I have an admin action that looks like this:

def process(modeladmin, request, queryset):
    for reservation in queryset:
        if not reservation.processed:
            reservation.processed = True
            reservation.save()
            item = reservation.item
            item.available = F('available') - reservation.quantity
            item.save()

So, the administrator can handle it reservation item. Whenever he does this, it is reservationmarked as processed, and the number of available is itemsreduced by the amount indicated in reservation.

As this happens for all administrator actions, the administrator can process several reservationsat a time. Everything goes well, if it reservationsis all different items. But if two reservationsare separated by one item, the number of available is itemsreduced only by the amount indicated in the last processed reservation.

, F() : item item . ?

+3
2

, F. , .

update, :

Item.objects.filter(id=reservation.item.id).update(available=F('available')-reservation.quantity)

- .

+1

F() , , SQL:

SELECT * FROM foo WHERE foo_col = bar_col

:

Foo.objects.filter(foo_col=F('bar_col'))

, , , , , . , , , "", .

-1

All Articles