-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
25 lines (17 loc) · 911 Bytes
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.db import models, transaction
class Product(models.Model):
name = models.CharField(max_length=1024)
price = models.DecimalField(max_digits=10, decimal_places=3)
inventory = models.PositiveIntegerField()
def purchase(self):
with transaction.atomic():
# We select the product again from the database, to ensure we have
# the latest version and we select it for update to ensure no one
# else can update the object while we modify it.
product = Product.objects.select_for_update().get(pk=self.pk)
# Check that we have at least one product in inventory. This is
# guaranteed to be correct because we have a lock on the row.
if product.inventory < 1:
raise ValueError('Inventory of %s is empty', product.name)
product.inventory -= 1
product.save()