from oscar.apps.basket import signals


def track_voucher_addition(basket, voucher, **kwargs):
    voucher.num_basket_additions += 1
    voucher.save()


def track_voucher_removal(basket, voucher, **kwargs):
    voucher.num_basket_additions -= 1
    voucher.save()


signals.voucher_addition.connect(track_voucher_addition)
signals.voucher_removal.connect(track_voucher_removal)

from django.db.models import F
from oscar.apps.basket import signals


def track_voucher_addition(basket, voucher, **kwargs):
    voucher.num_basket_additions += 1
    voucher.__class__._default_manager.filter(pk=voucher.pk).update(
        num_basket_additions=F('num_basket_additions') + 1,
    )


def track_voucher_removal(basket, voucher, **kwargs):
    voucher.num_basket_additions -= 1
    voucher.__class__._default_manager.filter(pk=voucher.pk).update(
        num_basket_additions=F('num_basket_additions') - 1,
    )


signals.voucher_addition.connect(track_voucher_addition)
signals.voucher_removal.connect(track_voucher_removal)

Fix race condition when tracking num_basket_additions on a voucher
Fix race condition when tracking num_basket_additions on a voucher

+ from django.db.models import F
  from oscar.apps.basket import signals
  
  
  def track_voucher_addition(basket, voucher, **kwargs):
      voucher.num_basket_additions += 1
-     voucher.save()
+     voucher.__class__._default_manager.filter(pk=voucher.pk).update(
+         num_basket_additions=F('num_basket_additions') + 1,
+     )
  
  
  def track_voucher_removal(basket, voucher, **kwargs):
      voucher.num_basket_additions -= 1
-     voucher.save()
+     voucher.__class__._default_manager.filter(pk=voucher.pk).update(
+         num_basket_additions=F('num_basket_additions') - 1,
+     )
  
  
  signals.voucher_addition.connect(track_voucher_addition)
  signals.voucher_removal.connect(track_voucher_removal)
  
Fix race condition when tracking num_basket_additions on a voucher
## Code Before:
from oscar.apps.basket import signals


def track_voucher_addition(basket, voucher, **kwargs):
    voucher.num_basket_additions += 1
    voucher.save()


def track_voucher_removal(basket, voucher, **kwargs):
    voucher.num_basket_additions -= 1
    voucher.save()


signals.voucher_addition.connect(track_voucher_addition)
signals.voucher_removal.connect(track_voucher_removal)

## Instruction:
Fix race condition when tracking num_basket_additions on a voucher
## Code After:
from django.db.models import F
from oscar.apps.basket import signals


def track_voucher_addition(basket, voucher, **kwargs):
    voucher.num_basket_additions += 1
    voucher.__class__._default_manager.filter(pk=voucher.pk).update(
        num_basket_additions=F('num_basket_additions') + 1,
    )


def track_voucher_removal(basket, voucher, **kwargs):
    voucher.num_basket_additions -= 1
    voucher.__class__._default_manager.filter(pk=voucher.pk).update(
        num_basket_additions=F('num_basket_additions') - 1,
    )


signals.voucher_addition.connect(track_voucher_addition)
signals.voucher_removal.connect(track_voucher_removal)
