本文介绍了Django post_save()信号执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于django的问题。



我在这里有ManyToMany模型

  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,via ='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。模型):
product = models.ForeignKey(Product)
cart = models.ForeignKey(Cart)
amount = models.IntegerField(default = 0)

对于1 cart对象创建,我可以插入与新的TransactionDetail对象(产品和金额)一样多。我的问题是。如何实现触发器?我想要的是每当一个交易细节被创建,我想要的产品的股票的数量减去交易详细的金额。



我已经阅读关于post_save( )但我不知道如何实现它。
可能是这样的



when:post_save(TransactionDetail,
Cart)#Cart对象,其中TransactionDetail.cart = Cart.id

  Cart.stock  -  = TransactionDetail.amount 


解决方案

如果你真的想使用信号来实现这一点,那么简单来说,

 django.db.models.signals导入post_save 
从django.dispatch导入接收器

class TransactionDetail(models.Model):
#...

#更新
@receiver(post_save,sender = TransactionDetail,dispatch_uid =update_stock_count)的方法
def update_stock(sender,instance,** kwargs):
instance.product.stock - = instance.amount
instance.product.save()


I have a question about django.

I have ManyToMany Models here

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)

For 1 cart object created, I can insert as many as new TransactionDetail object (the product and amount). My question is. How can I implement the trigger? What I want is whenever a Transaction detail is created, I want the amount of the product's stock is substracted by the amount in the transactiondetail.

I've read about post_save() but I'm not sure how to implement it.maybe something like this

when : post_save(TransactionDetail, Cart) #Cart object where TransactionDetail.cart= Cart.id

Cart.stock -= TransactionDetail.amount
解决方案

If you really want to use signals to achieve this, here's briefly how,

from django.db.models.signals import post_save
from django.dispatch import receiver

class TransactionDetail(models.Model):
    # ... fields here

# method for updating
@receiver(post_save, sender=TransactionDetail, dispatch_uid="update_stock_count")
def update_stock(sender, instance, **kwargs):
     instance.product.stock -= instance.amount
     instance.product.save()

这篇关于Django post_save()信号执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:56