django-bulk-hooks 0.2.66__py3-none-any.whl → 0.2.67__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of django-bulk-hooks might be problematic. Click here for more details.
- django_bulk_hooks/dispatcher.py +43 -19
- {django_bulk_hooks-0.2.66.dist-info → django_bulk_hooks-0.2.67.dist-info}/METADATA +1 -1
- {django_bulk_hooks-0.2.66.dist-info → django_bulk_hooks-0.2.67.dist-info}/RECORD +5 -5
- {django_bulk_hooks-0.2.66.dist-info → django_bulk_hooks-0.2.67.dist-info}/LICENSE +0 -0
- {django_bulk_hooks-0.2.66.dist-info → django_bulk_hooks-0.2.67.dist-info}/WHEEL +0 -0
django_bulk_hooks/dispatcher.py
CHANGED
|
@@ -287,23 +287,36 @@ class HookDispatcher:
|
|
|
287
287
|
"""
|
|
288
288
|
Preload relationships needed for condition evaluation.
|
|
289
289
|
|
|
290
|
+
This prevents N+1 queries when conditions access relationships on both
|
|
291
|
+
old_records and new_records (e.g., HasChanged conditions).
|
|
292
|
+
|
|
290
293
|
Args:
|
|
291
294
|
changeset: ChangeSet with records
|
|
292
295
|
relationships: Set of relationship field names to preload
|
|
293
296
|
"""
|
|
294
|
-
if not relationships
|
|
297
|
+
if not relationships:
|
|
295
298
|
return
|
|
296
299
|
|
|
297
300
|
# Use Django's select_related to preload relationships
|
|
298
301
|
relationship_list = list(relationships)
|
|
299
302
|
|
|
300
|
-
#
|
|
303
|
+
# Collect all unique PKs from both new_records and old_records
|
|
304
|
+
all_ids = set()
|
|
305
|
+
|
|
306
|
+
# Add PKs from new_records
|
|
301
307
|
if changeset.new_records:
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
308
|
+
all_ids.update(obj.pk for obj in changeset.new_records if obj.pk is not None)
|
|
309
|
+
|
|
310
|
+
# Add PKs from old_records
|
|
311
|
+
if changeset.old_records:
|
|
312
|
+
all_ids.update(obj.pk for obj in changeset.old_records if obj.pk is not None)
|
|
313
|
+
|
|
314
|
+
# Bulk preload relationships for all records that have PKs
|
|
315
|
+
if all_ids:
|
|
316
|
+
preloaded = changeset.model_cls.objects.filter(pk__in=list(all_ids)).select_related(*relationship_list).in_bulk()
|
|
317
|
+
|
|
318
|
+
# Update new_records with preloaded relationships
|
|
319
|
+
if changeset.new_records:
|
|
307
320
|
for obj in changeset.new_records:
|
|
308
321
|
if obj.pk and obj.pk in preloaded:
|
|
309
322
|
preloaded_obj = preloaded[obj.pk]
|
|
@@ -311,17 +324,27 @@ class HookDispatcher:
|
|
|
311
324
|
if hasattr(preloaded_obj, rel):
|
|
312
325
|
setattr(obj, rel, getattr(preloaded_obj, rel))
|
|
313
326
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
327
|
+
# Update old_records with preloaded relationships
|
|
328
|
+
if changeset.old_records:
|
|
329
|
+
for obj in changeset.old_records:
|
|
330
|
+
if obj.pk and obj.pk in preloaded:
|
|
331
|
+
preloaded_obj = preloaded[obj.pk]
|
|
332
|
+
for rel in relationship_list:
|
|
333
|
+
if hasattr(preloaded_obj, rel):
|
|
334
|
+
setattr(obj, rel, getattr(preloaded_obj, rel))
|
|
335
|
+
|
|
336
|
+
# Handle unsaved new_records by preloading their FK targets
|
|
337
|
+
if changeset.new_records:
|
|
338
|
+
for obj in changeset.new_records:
|
|
339
|
+
if obj.pk is None: # Unsaved object
|
|
340
|
+
for rel in relationship_list:
|
|
341
|
+
if hasattr(obj, f'{rel}_id'):
|
|
342
|
+
rel_id = getattr(obj, f'{rel}_id')
|
|
343
|
+
if rel_id:
|
|
344
|
+
# Load the related object
|
|
345
|
+
rel_model = getattr(changeset.model_cls._meta.get_field(rel).remote_field, 'model')
|
|
346
|
+
rel_obj = rel_model.objects.get(pk=rel_id)
|
|
347
|
+
setattr(obj, rel, rel_obj)
|
|
325
348
|
|
|
326
349
|
def _preload_select_related_for_before_create(self, changeset, select_related_fields):
|
|
327
350
|
"""
|
|
@@ -336,7 +359,8 @@ class HookDispatcher:
|
|
|
336
359
|
changeset: ChangeSet with new_records (unsaved objects)
|
|
337
360
|
select_related_fields: List of field names to preload (e.g., ['financial_account'])
|
|
338
361
|
"""
|
|
339
|
-
|
|
362
|
+
# Ensure select_related_fields is actually iterable (not a Mock in tests)
|
|
363
|
+
if not select_related_fields or not changeset.new_records or not hasattr(select_related_fields, '__iter__'):
|
|
340
364
|
return
|
|
341
365
|
|
|
342
366
|
logger.info(f"🔗 BULK PRELOAD: Preloading {select_related_fields} for {len(changeset.new_records)} unsaved records")
|
|
@@ -4,7 +4,7 @@ django_bulk_hooks/conditions.py,sha256=ar4pGjtxLKmgSIlO4S6aZFKmaBNchLtxMmWpkn4g9
|
|
|
4
4
|
django_bulk_hooks/constants.py,sha256=PxpEETaO6gdENcTPoXS586lerGKVP3nmjpDvOkmhYxI,509
|
|
5
5
|
django_bulk_hooks/context.py,sha256=mqaC5-yESDTA5ruI7fuXlt8qSgKuOFp0mjq7h1-4HdQ,1926
|
|
6
6
|
django_bulk_hooks/decorators.py,sha256=TdkO4FJyFrVU2zqK6Y_6JjEJ4v3nbKkk7aa22jN10sk,11994
|
|
7
|
-
django_bulk_hooks/dispatcher.py,sha256=
|
|
7
|
+
django_bulk_hooks/dispatcher.py,sha256=88emfa2Ae7uqBckjiodFa5E9L5lWfQLasSuEkzB4Qws,18273
|
|
8
8
|
django_bulk_hooks/enums.py,sha256=Zo8_tJzuzZ2IKfVc7gZ-0tWPT8q1QhqZbAyoh9ZVJbs,381
|
|
9
9
|
django_bulk_hooks/factory.py,sha256=ezrVM5U023KZqOBbJXb6lYUP-pE7WJmi8Olh2Ew-7RA,18085
|
|
10
10
|
django_bulk_hooks/handler.py,sha256=SRCrMzgolrruTkvMnYBFmXLR-ABiw0JiH3605PEdCZM,4207
|
|
@@ -21,7 +21,7 @@ django_bulk_hooks/operations/mti_plans.py,sha256=HIRJgogHPpm6MV7nZZ-sZhMLUnozpZP
|
|
|
21
21
|
django_bulk_hooks/operations/record_classifier.py,sha256=It85hJC2K-UsEOLbTR-QBdY5UPV-acQIJ91TSGa7pYo,7053
|
|
22
22
|
django_bulk_hooks/queryset.py,sha256=g_9OtOTC8FXY0hBwYr2FCqQ3mYXbfJTFPLlFV3SHmWQ,5600
|
|
23
23
|
django_bulk_hooks/registry.py,sha256=4HxP1mVK2z4VzvlohbEw2359wM21UJZJYagJJ1komM0,7947
|
|
24
|
-
django_bulk_hooks-0.2.
|
|
25
|
-
django_bulk_hooks-0.2.
|
|
26
|
-
django_bulk_hooks-0.2.
|
|
27
|
-
django_bulk_hooks-0.2.
|
|
24
|
+
django_bulk_hooks-0.2.67.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
|
|
25
|
+
django_bulk_hooks-0.2.67.dist-info/METADATA,sha256=s5bxGqotVtNJFn2zykkNZA0RbEYfYH9R3cU9299G46M,9265
|
|
26
|
+
django_bulk_hooks-0.2.67.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
27
|
+
django_bulk_hooks-0.2.67.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|