django 远程更新模型变量的值

ycggw6v2  于 2022-12-30  发布在  Go
关注(0)|答案(3)|浏览(129)

每当创建“PurchaseItem”对象时,我都希望更新“Item”中的库存值。

模型.py(1):

class PurchaseItem(models.Model):

product = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
purchase_price = models.DecimalField(max_digits=6, decimal_places=2)
paid_amount = models.DecimalField(max_digits=6, decimal_places=2)
date_created = models.DateTimeField(auto_now_add=True)

这里,quantity表示购买的物品数量,所以当我创建这个对象时;我希望在此处更新库存值:模型.py(2):

class Item(models.Model):
    title = models.CharField(max_length=100)
    model = models.CharField(max_length=100)
    sku = models.CharField(max_length=100)
    ean = models.CharField(max_length=100)
    price = models.FloatField()
    inventory = models.IntegerField()
pbgvytdp

pbgvytdp1#

首先,我想纠正另一个家伙谁回答有关related_name,它应该这样做:
添加related_name以便更好地查询:

class PurchaseItem(models.Model):

    product = models.ForeignKey(
        Item, on_delete=models.CASCADE, related_name="purchases"
    ) # here
    quantity = models.PositiveSmallIntegerField()
    purchase_price = models.DecimalField(max_digits=6, decimal_places=2)
    paid_amount = models.DecimalField(max_digits=6, decimal_places=2)
    date_created = models.DateTimeField(auto_now_add=True)

现在,从Item示例中,您可以获取所有以“product”作为Item示例的PurchaseItem示例,例如:

foo = Item.objects.get(title="FOO") # getting FOO

foo_purchases = foo.purchases.all() # getting all PurchaseItem objects that has "foo" as being the "product" attribute

但您不需要它来创建signals ...您需要做的只是:

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

@receiver(post_save, sender=PurchaseItem)
def update_item_inventory_on_create(sender, instance, created, **kwargs):

    """
    Everytime you create a PurchaseItem it will decrease the Item used as "Product" inventory
    based on the PurchaseItem quantity.

    Example: If my Product had 10 samples on inventory, and i create a PurchaseItem with 2 as quantity,
    after this signal is called, my product will now have only 8 samples on inventory.
    """

    if created:
        purchase_item_created = instance
        purchase_item_created.product.inventory -= purchase_item_created.quantity
        purchase_item_created.product.save()

@receiver(post_delete, sender=PurchaseItem)
def update_item_inventory_on_delete(sender, instance, **kwargs):
    """
    Everytime you delete a PurchaseItem it will increase the Item used as "Product" inventory
    based on the PurchaseItem quantity.

    Example: If my Product had 8 samples on inventory, and i have deleted a PurchaseItem with 2 as quantity,
    after this signal is called, my product will now have 10 samples on inventory.
    """

    purchase_item_created = instance
    purchase_item_created.product.inventory += purchase_item_created.quantity
    purchase_item_created.product.save()

请参考文档以获得更好的解释,我尽了最大努力使其简洁,并在信号文档字符串上进行了解释。
但是,使用该代码,您将能够在PurchaseItem创建时减少项目库存,并在PurchaseItem删除时增加项目库存

k97glaaz

k97glaaz2#

您可以使用signals或:
首先编辑PurchaseItem模型,并将related_name添加到product字段:

class PurchaseItem(models.Model):
    product = models.ForeignKey(Item,related_name="item", on_delete=models.CASCADE)
    quantity = models.PositiveSmallIntegerField()
    purchase_price = models.DecimalField(max_digits=6, decimal_places=2)
    paid_amount = models.DecimalField(max_digits=6, decimal_places=2)
    date_created = models.DateTimeField(auto_now_add=True)

现在,当您创建采购项目时:

purchase_item = PurchaseItem.objects.create(...)
purchase_item.item.inventory += purchase_item.quantity # or any number you want
purchase_item.item.save()

但尽量使用信号。

bvpmtnay

bvpmtnay3#

使用Django,有三种方法:
1.创建一个每当创建PurchaseItem时都会调用的信号接收器,该信号可以调用处理更新逻辑的方法。
1.重写PurchaseItemsave()方法,检查是否正在创建或更新对象,然后调用另一个处理更新逻辑的方法。
1.您也可以有一个视图调用更新逻辑,但是您可能希望尽可能地将业务隐藏在视图之外。
希望这对你有帮助

相关问题