def _update_counter(model, field_name, filter_kwargs, increment=1):
    try:
        record = model.objects.filter(**filter_kwargs)
        affected = record.update(**{field_name: F(field_name) + increment})
        if not affected:
            filter_kwargs[field_name] = increment
            model.objects.create(**filter_kwargs)
    except IntegrityError:
        # get_or_create sometimes fails due to MySQL's weird transactions, fail
        # silently
        logger.error(
            "IntegrityError when updating analytics counter for %s", model)
Efficiently updates a counter field by a given increment. Uses Django's
dataset/ETHPy150Open django-oscar/django-oscar/src/oscar/apps/analytics/receivers.py/_update_counter