django orm正在创建实例的副本,而不是访问它
我认为在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。
cartitemproduct_in_cart_session.get
等于:
is equivalent to:
while
is equivalent to: