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: