(二十八)加锁
django 要加锁 必须先加事物
例子
from rest_framework import exceptions
from django.db import transaction
def perform_create(self, serializer): with transaction.atomic(): price = self.request.data.get('price') item_id = self.request.data.get('item') result = models.BidRecord.objects.filter(item_id=item_id).aggregate(max_price=Max('price')).select_for_update() max_price = result['max_price'] if price > max_price: serializer.save(user=self.request.user) raise exceptions.ValidationError('已经被出价了,再涨涨.')