summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2019-11-29 18:19:29 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-03 19:47:32 +0100
commitc50839fccf8b3235624b70b2c263157e570dbc2a (patch)
tree40f3753f5d4dadefd4e1b59327d5ba1d7bfd9b27
parent6c0341f127c4d0772aff8e187e620fcb300fa9a9 (diff)
Simplified RelatedManager._add_items() a bit.
Added early return in RelatedManager._add_items() to decrease an indentation level.
-rw-r--r--django/db/models/fields/related_descriptors.py79
1 files changed, 39 insertions, 40 deletions
diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py
index ec603bc8af..cc1721c9e5 100644
--- a/django/db/models/fields/related_descriptors.py
+++ b/django/db/models/fields/related_descriptors.py
@@ -1113,49 +1113,48 @@ def create_forward_many_to_many_manager(superclass, rel, reverse):
# source_field_name: the PK fieldname in join table for the source object
# target_field_name: the PK fieldname in join table for the target object
# *objs - objects to add. Either object instances, or primary keys of object instances.
- through_defaults = through_defaults or {}
-
- # If there aren't any objects, there is nothing to do.
- if objs:
- target_ids = self._get_target_ids(target_field_name, objs)
- db = router.db_for_write(self.through, instance=self.instance)
- can_ignore_conflicts, must_send_signals, can_fast_add = self._get_add_plan(db, source_field_name)
- if can_fast_add:
- self.through._default_manager.using(db).bulk_create([
- self.through(**{
- '%s_id' % source_field_name: self.related_val[0],
- '%s_id' % target_field_name: target_id,
- })
- for target_id in target_ids
- ], ignore_conflicts=True)
- return
+ if not objs:
+ return
- missing_target_ids = self._get_missing_target_ids(
- source_field_name, target_field_name, db, target_ids
- )
- with transaction.atomic(using=db, savepoint=False):
- if must_send_signals:
- signals.m2m_changed.send(
- sender=self.through, action='pre_add',
- instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=missing_target_ids, using=db,
- )
+ through_defaults = through_defaults or {}
+ target_ids = self._get_target_ids(target_field_name, objs)
+ db = router.db_for_write(self.through, instance=self.instance)
+ can_ignore_conflicts, must_send_signals, can_fast_add = self._get_add_plan(db, source_field_name)
+ if can_fast_add:
+ self.through._default_manager.using(db).bulk_create([
+ self.through(**{
+ '%s_id' % source_field_name: self.related_val[0],
+ '%s_id' % target_field_name: target_id,
+ })
+ for target_id in target_ids
+ ], ignore_conflicts=True)
+ return
- # Add the ones that aren't there already.
- self.through._default_manager.using(db).bulk_create([
- self.through(**through_defaults, **{
- '%s_id' % source_field_name: self.related_val[0],
- '%s_id' % target_field_name: target_id,
- })
- for target_id in missing_target_ids
- ], ignore_conflicts=can_ignore_conflicts)
+ missing_target_ids = self._get_missing_target_ids(
+ source_field_name, target_field_name, db, target_ids
+ )
+ with transaction.atomic(using=db, savepoint=False):
+ if must_send_signals:
+ signals.m2m_changed.send(
+ sender=self.through, action='pre_add',
+ instance=self.instance, reverse=self.reverse,
+ model=self.model, pk_set=missing_target_ids, using=db,
+ )
+ # Add the ones that aren't there already.
+ self.through._default_manager.using(db).bulk_create([
+ self.through(**through_defaults, **{
+ '%s_id' % source_field_name: self.related_val[0],
+ '%s_id' % target_field_name: target_id,
+ })
+ for target_id in missing_target_ids
+ ], ignore_conflicts=can_ignore_conflicts)
- if must_send_signals:
- signals.m2m_changed.send(
- sender=self.through, action='post_add',
- instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=missing_target_ids, using=db,
- )
+ if must_send_signals:
+ signals.m2m_changed.send(
+ sender=self.through, action='post_add',
+ instance=self.instance, reverse=self.reverse,
+ model=self.model, pk_set=missing_target_ids, using=db,
+ )
def _remove_items(self, source_field_name, target_field_name, *objs):
# source_field_name: the PK colname in join table for the source object