Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Select for update stocks

Mira Szewczyk7b7c755639cc
diff --git a/saleor/stock/management.py b/saleor/stock/management.py
index 671dbbd080..22d4098427 100644
--- a/saleor/stock/management.py
+++ b/saleor/stock/management.py
@@ -1,27 +1,36 @@
from typing import TYPE_CHECKING
+from django.db import transaction
+
from .models import Stock
if TYPE_CHECKING:
from ..product.models import ProductVariant
def allocate_stock(
variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
) -> Stock:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(
+ country_code, variant
+ )
stock.allocate_stock(quantity, commit=commit)
return stock
def deallocate_stock(
variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
) -> Stock:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(
+ country_code, variant
+ )
stock.deallocate_stock(quantity, commit=commit)
return stock
def increase_stock(
variant: "ProductVariant",
country_code: str,
@@ -29,14 +38,19 @@ def increase_stock(
allocate: bool = False,
commit: bool = True,
) -> Stock:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(
+ country_code, variant
+ )
stock.increase_stock(quantity, allocate=allocate, commit=commit)
return stock
def decrease_stock(
variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
) -> Stock:
- stock = Stock.objects.for_country(country_code).get(product_variant=variant)
+ stock = Stock.objects.select_for_update(of=("self",)).get_variant_stock_for_country(
+ country_code, variant
+ )
stock.decrease_stock(quantity, commit=commit)
return stock