django-bulk-hooks 0.2.73__py3-none-any.whl → 0.2.74__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.

@@ -409,17 +409,38 @@ class HookDispatcher:
409
409
  if hasattr(preloaded_obj, rel):
410
410
  setattr(obj, rel, getattr(preloaded_obj, rel))
411
411
 
412
- # Handle unsaved new_records by preloading their FK targets
412
+ # Handle unsaved new_records by preloading their FK targets (bulk query to avoid N+1)
413
413
  if changeset.new_records:
414
+ # Collect FK IDs for each relationship from unsaved records
415
+ field_ids_map = {rel: set() for rel in relationship_list}
416
+
414
417
  for obj in changeset.new_records:
415
418
  if obj.pk is None: # Unsaved object
416
419
  for rel in relationship_list:
417
420
  if hasattr(obj, f'{rel}_id'):
418
421
  rel_id = getattr(obj, f'{rel}_id')
419
422
  if rel_id:
420
- # Load the related object
421
- rel_model = getattr(changeset.model_cls._meta.get_field(rel).remote_field, 'model')
422
- rel_obj = rel_model.objects.get(pk=rel_id)
423
+ field_ids_map[rel].add(rel_id)
424
+
425
+ # Bulk load relationships for unsaved records
426
+ field_objects_map = {}
427
+ for rel, ids in field_ids_map.items():
428
+ if not ids:
429
+ continue
430
+ try:
431
+ rel_model = getattr(changeset.model_cls._meta.get_field(rel).remote_field, 'model')
432
+ field_objects_map[rel] = rel_model.objects.in_bulk(ids)
433
+ except Exception:
434
+ field_objects_map[rel] = {}
435
+
436
+ # Attach relationships to unsaved records
437
+ for obj in changeset.new_records:
438
+ if obj.pk is None: # Unsaved object
439
+ for rel in relationship_list:
440
+ rel_id = getattr(obj, f'{rel}_id', None)
441
+ if rel_id and rel in field_objects_map:
442
+ rel_obj = field_objects_map[rel].get(rel_id)
443
+ if rel_obj:
423
444
  setattr(obj, rel, rel_obj)
424
445
 
425
446
  def _preload_select_related_for_before_create(self, changeset, select_related_fields):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: django-bulk-hooks
3
- Version: 0.2.73
3
+ Version: 0.2.74
4
4
  Summary: Hook-style hooks for Django bulk operations like bulk_create and bulk_update.
5
5
  License: MIT
6
6
  Keywords: django,bulk,hooks
@@ -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=gQ8LYATpgYInKk-zzumV8g0Uq3tLh9arRUBR2rYvOp8,22724
7
+ django_bulk_hooks/dispatcher.py,sha256=LfqSDE1NK-cgAUDwaN542_d4ohSxBeiOAhbOqklZfPo,23659
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=tHt1U2O9Hvozg9sdn5MjzAk_I6wDU-LupuKFTfv1SiQ,7449
23
23
  django_bulk_hooks/registry.py,sha256=4HxP1mVK2z4VzvlohbEw2359wM21UJZJYagJJ1komM0,7947
24
- django_bulk_hooks-0.2.73.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
25
- django_bulk_hooks-0.2.73.dist-info/METADATA,sha256=6bVemHt8jIKA8HKockPGTjjnh1KZVWMb3MJnzwoLdto,10555
26
- django_bulk_hooks-0.2.73.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
27
- django_bulk_hooks-0.2.73.dist-info/RECORD,,
24
+ django_bulk_hooks-0.2.74.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
25
+ django_bulk_hooks-0.2.74.dist-info/METADATA,sha256=TKjaRj-VJoMK_WiZVl7TNPf9yay5he233hmsMAFmYJI,10555
26
+ django_bulk_hooks-0.2.74.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
27
+ django_bulk_hooks-0.2.74.dist-info/RECORD,,