Used defaultdict in deletion.Collector.

This commit is contained in:
Simon Charette 2019-10-06 13:56:15 +02:00 committed by Mariusz Felisiak
parent 04ac9b45a3
commit 832aa08afe
1 changed files with 9 additions and 10 deletions

View File

@ -1,4 +1,5 @@
from collections import Counter from collections import Counter, defaultdict
from functools import partial
from itertools import chain from itertools import chain
from operator import attrgetter from operator import attrgetter
@ -65,8 +66,9 @@ class Collector:
def __init__(self, using): def __init__(self, using):
self.using = using self.using = using
# Initially, {model: {instances}}, later values become lists. # Initially, {model: {instances}}, later values become lists.
self.data = {} self.data = defaultdict(set)
self.field_updates = {} # {model: {(field, value): {instances}}} # {model: {(field, value): {instances}}}
self.field_updates = defaultdict(partial(defaultdict, set))
# fast_deletes is a list of queryset-likes that can be deleted without # fast_deletes is a list of queryset-likes that can be deleted without
# fetching the objects into memory. # fetching the objects into memory.
self.fast_deletes = [] self.fast_deletes = []
@ -76,7 +78,7 @@ class Collector:
# should be included, as the dependencies exist only between actual # should be included, as the dependencies exist only between actual
# database tables; proxy models are represented here by their concrete # database tables; proxy models are represented here by their concrete
# parent. # parent.
self.dependencies = {} # {model: {models}} self.dependencies = defaultdict(set) # {model: {models}}
def add(self, objs, source=None, nullable=False, reverse_dependency=False): def add(self, objs, source=None, nullable=False, reverse_dependency=False):
""" """
@ -90,7 +92,7 @@ class Collector:
return [] return []
new_objs = [] new_objs = []
model = objs[0].__class__ model = objs[0].__class__
instances = self.data.setdefault(model, set()) instances = self.data[model]
for obj in objs: for obj in objs:
if obj not in instances: if obj not in instances:
new_objs.append(obj) new_objs.append(obj)
@ -101,8 +103,7 @@ class Collector:
if source is not None and not nullable: if source is not None and not nullable:
if reverse_dependency: if reverse_dependency:
source, model = model, source source, model = model, source
self.dependencies.setdefault( self.dependencies[source._meta.concrete_model].add(model._meta.concrete_model)
source._meta.concrete_model, set()).add(model._meta.concrete_model)
return new_objs return new_objs
def add_field_update(self, field, value, objs): def add_field_update(self, field, value, objs):
@ -113,9 +114,7 @@ class Collector:
if not objs: if not objs:
return return
model = objs[0].__class__ model = objs[0].__class__
self.field_updates.setdefault( self.field_updates[model][field, value].update(objs)
model, {}).setdefault(
(field, value), set()).update(objs)
def _has_signal_listeners(self, model): def _has_signal_listeners(self, model):
return ( return (