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 the operation. IfFalse
(the default), the elements missing from the new set are removed usingremove()
and only the new ones are added. Ifclear=True
, theclear()
method is called instead and the whole set is added 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/