MCPcopy Create free account
hub / github.com/tortoise/tortoise-orm / BulkUpdateQuery

Class BulkUpdateQuery

tortoise/queryset.py:1918–2007  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1916
1917
1918class BulkUpdateQuery(UpdateQuery, Generic[MODEL]):
1919 __slots__ = ("fields", "_objects", "_batch_size", "_queries")
1920
1921 def __init__(
1922 self,
1923 model: type[MODEL],
1924 db: BaseDBAsyncClient,
1925 q_objects: list[Q],
1926 annotations: dict[str, Any],
1927 custom_filters: dict[str, FilterInfoDict],
1928 limit: int | None,
1929 orderings: list[tuple[str, str]],
1930 objects: Iterable[MODEL],
1931 fields: Iterable[str],
1932 batch_size: int | None = None,
1933 ):
1934 super().__init__(
1935 model,
1936 update_kwargs={},
1937 db=db,
1938 q_objects=q_objects,
1939 annotations=annotations,
1940 custom_filters=custom_filters,
1941 limit=limit,
1942 orderings=orderings,
1943 )
1944 self.fields = fields
1945 self._objects = objects
1946 self._batch_size = batch_size
1947 self._queries: list[QueryBuilder] = []
1948
1949 def _make_queries(self) -> list[tuple[str, list[Any]]]:
1950 table = self.model._meta.basetable
1951 self.query = self._db.query_class.update(table)
1952 if self.capabilities.support_update_limit_order_by and self._limit:
1953 self.query._limit = self.query._wrapper_cls(self._limit)
1954 self.resolve_ordering(
1955 model=self.model,
1956 table=table,
1957 orderings=self._orderings,
1958 annotations=self._annotations,
1959 )
1960
1961 self.resolve_filters()
1962 pk_attr = self.model._meta.pk_attr
1963 source_pk_attr = self.model._meta.fields_map[pk_attr].source_field or pk_attr
1964 pk = Field(source_pk_attr)
1965 for objects_item in chunk(self._objects, self._batch_size):
1966 query = copy(self.query)
1967 for field in self.fields:
1968 case = Case()
1969 pk_list = []
1970 for obj in objects_item:
1971 pk_value = self.model._meta.fields_map[pk_attr].to_db_value(obj.pk, None)
1972 field_obj = obj._meta.fields_map[field]
1973 field_value = field_obj.to_db_value(getattr(obj, field), obj)
1974 case.when(
1975 pk == pk_value,

Callers 1

bulk_updateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…