django orm正在创建实例的副本,而不是访问它

发布于 2025-02-02 03:16:10 字数 836 浏览 4 评论 0原文

我认为在Django中,我认为这是一个棘手的问题,这是ORM。

不起作用:

cartitemproduct_in_cart_session.get().quantity+=1

cartitemproduct_in_cart_session.get().save()

在此之后检查一下cartitemproduct_in_cart_session.get()的值。

cartitem_session=cartitemproduct_in_cart_session.get()

cartitem_session.quantity+=1

cartitem_session.save()

如果

我 >But why ?

(cartitemproduct_in_cart_session is a queryset, result of a filter, but I think it doesn't matter : cartitemproduct_in_cart_session=cart_session.cartitem_set.filter(product__slug=cartitem.product.slug

我猜想,当我做cartitemproduct_in_cart_session.get()。数量时,字段数量成为cartitemproduct_in_cart_session.get()的新属性,但我不再链接到数据库中的字段,但我不再了解为什么...

为什么您需要首先将模型的实例分配给名称,以更新该实例的字段?

I have I think kind of a tricky question in Django and it's orm.

This does not work :

cartitemproduct_in_cart_session.get().quantity+=1

cartitemproduct_in_cart_session.get().save()

If I check just after that the value of cartitemproduct_in_cart_session.get().quantity, it wasn't updated

This works :

cartitem_session=cartitemproduct_in_cart_session.get()

cartitem_session.quantity+=1

cartitem_session.save()

The value was updated

But why ?

(cartitemproduct_in_cart_session is a queryset, result of a filter, but I think it doesn't matter : cartitemproduct_in_cart_session=cart_session.cartitem_set.filter(product__slug=cartitem.product.slug) )

I am guessing that somehow, when I do cartitemproduct_in_cart_session.get().quantity, the field quantity becomes a new attributes of cartitemproduct_in_cart_session.get() and isn't linked anymore to the field in the database, but I don't understand why ...

Why do you need to first assign an instance of a model to a name, in order to update the fields of that instance ?

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

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

发布评论

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

评论(1

贪恋 2025-02-09 03:16:10

cartitemproduct_in_cart_session.get()。数量+= 1

cartitemproduct_in_cart_session.get()。保存()

x = cartitemproduct_in_cart_session.get()
x.quantity += 1

y = cartitemproduct_in_cart_session.get()
y.save()
# note that x and y are different objects with different memory addresses

cartitemproduct_in_cart_session.get

cartitem_session = cartitemproduct_in_cart_session.get()

cartitem_session.quantity+= 1

cartitem_session.save()

等于:

x = cartitemproduct_in_cart_session.get()
x.quantity += 1
x.save()

cartitemproduct_in_cart_session.get().quantity+=1

cartitemproduct_in_cart_session.get().save()

is equivalent to:

x = cartitemproduct_in_cart_session.get()
x.quantity += 1

y = cartitemproduct_in_cart_session.get()
y.save()
# note that x and y are different objects with different memory addresses

while

cartitem_session=cartitemproduct_in_cart_session.get()

cartitem_session.quantity+=1

cartitem_session.save()

is equivalent to:

x = cartitemproduct_in_cart_session.get()
x.quantity += 1
x.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文