如何在管理模型的保存方法中使用select_reded

发布于 2025-02-13 05:48:24 字数 452 浏览 3 评论 0原文

我想从不同模型中获取特定信息,因此我使用select_reced的方法获取数据,但我无法将记录保存到db中。 如何在保存方法中使用select_reced?

假设我有以下模型 models.py.py

现在我想从StockList中获得product_quantity shipping_qty(来自库存型号)以获取剩余的最终数量。

admin.py

但是当我使用时,我无法将值保存/更新为db select_reced以获取特定信息。我该怎么做?谢谢。

I would like to get the specific information from different model therefore I have use the method of select_related to fetch the data but I unable to save the record into db.
How to use select_related in save method?

Let say I have following models
models.py

Now I would like to get the product_quantity from stocklist and do a subtraction between product_quantity and shipping_qty(from stockOut models) to get the final quantity that remain.

admin.py

But I unable to save/update the value into db when I use the method of select_related to get the specific information. How would I go about doing this? Thanks.

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

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

发布评论

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

评论(1

嗫嚅 2025-02-20 05:48:24

首先,如果您执行stockout.object.all()。select_reled(“ shipts _qty”),您将不仅获得shipping_qty 和库存

您想要的是,仅获取这些值

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.shipping_qty = stokc.quantity_in_store - shipping_qty
    super().save_model(request, obj, form, change)

first of all you don't get only shipping_qty if you do stockOut.objects.all().select_related("shipping_qty") but you get all stockOut instances with their shipping_qty and stocks

what you want is, to get these values for only the current instance

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.shipping_qty = stokc.quantity_in_store - shipping_qty
    super().save_model(request, obj, form, change)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文