MCPcopy
hub / github.com/ormar-orm/ormar / update

Method update

ormar/queryset/queryset.py:874–909  ·  view source on GitHub ↗

Updates the model table after applying the filters from kwargs. You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect whole table. :param each: flag if whole table should be affected if no filter is passed :type

(self, each: bool = False, **kwargs: Any)

Source from the content-addressed store, hash-verified

872 return await self._query_aggr_function(func_name="avg", columns=columns)
873
874 async def update(self, each: bool = False, **kwargs: Any) -> int:
875 """
876 Updates the model table after applying the filters from kwargs.
877
878 You have to either pass a filter to narrow down a query or explicitly pass
879 each=True flag to affect whole table.
880
881 :param each: flag if whole table should be affected if no filter is passed
882 :type each: bool
883 :param kwargs: fields names and proper value types
884 :type kwargs: Any
885 :return: number of updated rows
886 :rtype: int
887 """
888 if not each and not (self.filter_clauses or self.exclude_clauses):
889 raise QueryDefinitionError(
890 "You cannot update without filtering the queryset first. "
891 "If you want to update all rows use update(each=True, **kwargs)"
892 )
893
894 self_fields = self.model.extract_db_own_fields().union(
895 self.model.extract_related_names()
896 )
897 updates = {k: v for k, v in kwargs.items() if k in self_fields}
898 updates = self.model.populate_onupdate_value(updates)
899 updates = self.model.validate_enums(updates)
900 updates = self.model.translate_columns_to_aliases(updates)
901
902 expr = FilterQuery(filter_clauses=self.filter_clauses).apply(
903 self.table.update().values(**updates) # type: ignore
904 )
905 expr = FilterQuery(filter_clauses=self.exclude_clauses, exclude=True).apply(
906 expr
907 )
908 async with self.model_config.database.get_query_executor() as executor:
909 return await executor.execute(expr)
910
911 async def delete(self, *args: Any, each: bool = False, **kwargs: Any) -> int:
912 """

Callers 2

update_or_createMethod · 0.45
bulk_updateMethod · 0.45

Calls 11

FilterQueryClass · 0.90
extract_db_own_fieldsMethod · 0.80
extract_related_namesMethod · 0.80
validate_enumsMethod · 0.80
get_query_executorMethod · 0.80
executeMethod · 0.80
applyMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected