장고에 대한 질문이 있습니다. Django post_save() 신호 구현
은 내가 만든 1 카트 목적은 여기를class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2)
stock = models.IntegerField(default=0)
def __unicode__(self):
return self.name
class Cart(models.Model):
customer = models.ForeignKey(Customer)
products = models.ManyToManyField(Product, through='TransactionDetail')
t_date = models.DateField(default=datetime.now())
t_sum = models.FloatField(default=0.0)
def __unicode__(self):
return str(self.id)
class TransactionDetail(models.Model):
product = models.ForeignKey(Product)
cart = models.ForeignKey(Cart)
amount = models.IntegerField(default=0)
ManyToMany 모델을 가지고, 나는 새로운 TransactionDetail 개체 (제품 및 금액)만큼을 삽입 할 수 있습니다. 내 질문은. 방아쇠를 어떻게 구현할 수 있습니까? 내가 원하는 것은 거래 세부 사항이 생성 될 때마다, 나는 제품의 재고 금액을 transactiondetail의 금액으로 뺀 것을 원한다.
post_save()에 대해 읽었지만 구현 방법을 잘 모르겠습니다. 어쩌면이
같은 때 : post_save (TransactionDetail, 장바구니) TransactionDetail.cart = Cart.id
Cart.stock -= TransactionDetail.amount
이렇게하면 경주에 빠질 가능성이 큽니다. 나는의 인스턴스 자체를 저장했기 때문에 –