Created
April 30, 2014 03:01
-
-
Save bendavis78/fd821b461eab3242ce39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update(self, **kwargs): | |
""" | |
Updates all elements in the current QuerySet, setting all the given | |
fields to the appropriate values. | |
""" | |
assert self.query.can_filter(), \ | |
"Cannot update a query once a slice has been taken." | |
meta = self.model._meta | |
self._for_write = True | |
query = self.query.clone(sql.UpdateQuery) | |
query.add_update_values(kwargs) | |
extra_data = {} | |
if not meta.auto_created: | |
responses = signals.pre_update.send( | |
sender=self.model, update_fields=kwargs, queryset=self, | |
using=self.db) | |
for rcvr, response in responses: | |
extra_data.update(response) | |
with transaction.atomic(using=self.db, savepoint=False): | |
rows = query.get_compiler(self.db).execute_sql(CURSOR) | |
self._result_cache = None | |
if not meta.auto_created: | |
signals.post_update.send( | |
sender=self.model, update_fields=kwargs, | |
extra_data=extra_data, using=self.db) | |
return rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment