From c50839fccf8b3235624b70b2c263157e570dbc2a Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Fri, 29 Nov 2019 18:19:29 +0100 Subject: [PATCH] Simplified RelatedManager._add_items() a bit. Added early return in RelatedManager._add_items() to decrease an indentation level. --- .../db/models/fields/related_descriptors.py | 79 +++++++++---------- 1 file 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. + if not objs: + return + 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 - # 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 + 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) - 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