如何更新外国钥匙值django

发布于 2025-02-13 12:16:13 字数 1216 浏览 0 评论 0原文

假设我有以下型号(模型

class stockList(models.Model):
 product_name = models.CharField(max_length=255, null=True, unique=True)
 product_image = models.ImageField(upload_to='photos')
 product_type = models.CharField(max_length=50, null=True)
 product_description = models.TextField(null=True)
 quantity_in_store = models.IntegerField(null=True)
 location = models.CharField(max_length=50, null=True)

class stockOut(models.Model):
 staff_name = models.ForeignKey("staff", on_delete=models.CASCADE, null=True)
 stock = models.ForeignKey("stockList", on_delete=models.CASCADE, null=True)
 shipping_qty = models.IntegerField(null=True)
 shipping_company = models.CharField(max_length=255, null=True)

。获取最终数量,但我想将最终数量的价值更新到dentity_in_store(来自StockList模型),我该如何执行此操作

class stockout(admin.ModelAdmin):
list_display = ["staff_name", "stock", "shipping_qty", "shipping_company"]

def save_model(self, request, obj, form, change):
    stock = obj.stock
    shipping_qty = obj.shipping_qty
    if shipping_qty and stock.quantity_in_store:
        obj.stock.quantity_in_store = stock.quantity_in_store - shipping_qty
    super().save_model(request, obj, form, change)

Let say I have following models (models.py):

class stockList(models.Model):
 product_name = models.CharField(max_length=255, null=True, unique=True)
 product_image = models.ImageField(upload_to='photos')
 product_type = models.CharField(max_length=50, null=True)
 product_description = models.TextField(null=True)
 quantity_in_store = models.IntegerField(null=True)
 location = models.CharField(max_length=50, null=True)

class stockOut(models.Model):
 staff_name = models.ForeignKey("staff", on_delete=models.CASCADE, null=True)
 stock = models.ForeignKey("stockList", on_delete=models.CASCADE, null=True)
 shipping_qty = models.IntegerField(null=True)
 shipping_company = models.CharField(max_length=255, null=True)

First of all I have to do a subtraction between quantity_in_store (from stockList models) and shipping_qty(from stockout models) and to get the final quantity that left in store. However, I have get the final quantity but I would like to update the value of final quantity into the quantity_in_store(from stockList models) How would I go about doing this? Thanks

admin models:

class stockout(admin.ModelAdmin):
list_display = ["staff_name", "stock", "shipping_qty", "shipping_company"]

def save_model(self, request, obj, form, change):
    stock = obj.stock
    shipping_qty = obj.shipping_qty
    if shipping_qty and stock.quantity_in_store:
        obj.stock.quantity_in_store = stock.quantity_in_store - shipping_qty
    super().save_model(request, obj, form, change)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2025-02-20 12:16:13

尽管保存的检查数量是一种不好的做法,但应该在验证时完成,您可以使用您的代码:

def save_model(self, request, obj, form, change):
    stock = obj.stock
    shipping_qty = obj.shipping_qty
    if shipping_qty and stock.quantity_in_store:
        obj.stock.quantity_in_store = stock.quantity_in_store - shipping_qty
        # don't forget to save stock after change quantity
        obj.stock.save()
    super().save_model(request, obj, form, change)

Although check quantity on save is a bad practice, it should be done on validation, you can use your code:

def save_model(self, request, obj, form, change):
    stock = obj.stock
    shipping_qty = obj.shipping_qty
    if shipping_qty and stock.quantity_in_store:
        obj.stock.quantity_in_store = stock.quantity_in_store - shipping_qty
        # don't forget to save stock after change quantity
        obj.stock.save()
    super().save_model(request, obj, form, change)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文