diff options
| author | Loic Bistuer <loic.bistuer@gmail.com> | 2015-02-05 16:52:26 +0700 |
|---|---|---|
| committer | Loic Bistuer <loic.bistuer@gmail.com> | 2015-02-05 16:59:56 +0700 |
| commit | 20eb51ce0ddcb8d5fdc772fc2839fefef5b28c2a (patch) | |
| tree | cc5140a79d492ae7f1950450b0ec970f2006df15 | |
| parent | 9d104a21e20f9c5ec41d19fd919d0e808aa13dba (diff) | |
Fix small regression caused by 71ada3a8e689a883b5ffdeb1744ea16f176ab730.
During direct assignment, evaluating the iterable before the transaction
is started avoids leaving the transaction dirty if an exception is raised.
This is slightly more wasteful but probably not enough to warrant a change
of behavior.
Thanks Anssi for the feedback. Refs #6707.
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 8 | ||||
| -rw-r--r-- | django/db/models/fields/related.py | 16 |
2 files changed, 12 insertions, 12 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 6a316e8d9a..b20d38768d 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -554,15 +554,15 @@ def create_generic_related_manager(superclass): _clear.alters_data = True def set(self, objs, **kwargs): + # Force evaluation of `objs` in case it's a queryset whose value + # could be affected by `manager.clear()`. Refs #19816. + objs = tuple(objs) + clear = kwargs.pop('clear', False) db = router.db_for_write(self.model, instance=self.instance) with transaction.atomic(using=db, savepoint=False): if clear: - # Force evaluation of `objs` in case it's a queryset whose value - # could be affected by `manager.clear()`. Refs #19816. - objs = tuple(objs) - self.clear() self.add(*objs) else: diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 0b0414bb0f..40e1028f1c 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -797,16 +797,16 @@ def create_foreign_related_manager(superclass, rel_field, rel_model): _clear.alters_data = True def set(self, objs, **kwargs): + # Force evaluation of `objs` in case it's a queryset whose value + # could be affected by `manager.clear()`. Refs #19816. + objs = tuple(objs) + clear = kwargs.pop('clear', False) if rel_field.null: db = router.db_for_write(self.model, instance=self.instance) with transaction.atomic(using=db, savepoint=False): if clear: - # Force evaluation of `objs` in case it's a queryset whose value - # could be affected by `manager.clear()`. Refs #19816. - objs = tuple(objs) - self.clear() self.add(*objs) else: @@ -1029,15 +1029,15 @@ def create_many_related_manager(superclass, rel): (opts.app_label, opts.object_name) ) + # Force evaluation of `objs` in case it's a queryset whose value + # could be affected by `manager.clear()`. Refs #19816. + objs = tuple(objs) + clear = kwargs.pop('clear', False) db = router.db_for_write(self.through, instance=self.instance) with transaction.atomic(using=db, savepoint=False): if clear: - # Force evaluation of `objs` in case it's a queryset whose value - # could be affected by `manager.clear()`. Refs #19816. - objs = tuple(objs) - self.clear() self.add(*objs) else: |
