mirror of
https://github.com/django/django.git
synced 2025-01-10 18:36:05 +00:00
Simplified RelatedManager._add_items() a bit.
Added early return in RelatedManager._add_items() to decrease an indentation level.
This commit is contained in:
parent
6c0341f127
commit
c50839fccf
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user