gpt4 book ai didi

django - Django ManyToMany 字段的 set() 操作是否清除现有关系?

转载 作者:行者123 更新时间:2023-12-05 02:05:35 25 4
gpt4 key购买 nike

Django ORM 操作中下面2种情况有区别吗?会有任何性能提升等吗?

obj.manytomanyfield.clear()
obj.manytomanyfield.add(1,2,3,4,5)

obj.manytomanyfield.set([1,2,3,4,5])

最佳答案

来自文档,set()-Django doc

This method accepts a clear argument to control how to perform theoperation. If False (the default), the elements missing from the newset are removed using remove() and only the new ones are added. Ifclear=True, the clear() method is called instead and the whole set isadded at once.

这意味着,

obj.manytomanyfield.clear()
obj.manytomanyfield.add(1,2,3,4,5)

等于

obj.manytomanyfield.set([1,2,3,4,5], <b>clear=True</b>)

关于django - Django ManyToMany 字段的 set() 操作是否清除现有关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63412222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com