django-bulk-hooks 0.2.44__py3-none-any.whl → 0.2.93__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.
- django_bulk_hooks/__init__.py +0 -3
- django_bulk_hooks/changeset.py +214 -230
- django_bulk_hooks/conditions.py +7 -3
- django_bulk_hooks/decorators.py +5 -15
- django_bulk_hooks/dispatcher.py +546 -242
- django_bulk_hooks/handler.py +2 -2
- django_bulk_hooks/helpers.py +258 -100
- django_bulk_hooks/manager.py +134 -130
- django_bulk_hooks/models.py +89 -75
- django_bulk_hooks/operations/analyzer.py +466 -315
- django_bulk_hooks/operations/bulk_executor.py +608 -413
- django_bulk_hooks/operations/coordinator.py +601 -454
- django_bulk_hooks/operations/field_utils.py +335 -0
- django_bulk_hooks/operations/mti_handler.py +696 -511
- django_bulk_hooks/operations/mti_plans.py +103 -96
- django_bulk_hooks/operations/record_classifier.py +35 -23
- django_bulk_hooks/queryset.py +60 -15
- django_bulk_hooks/registry.py +0 -2
- {django_bulk_hooks-0.2.44.dist-info → django_bulk_hooks-0.2.93.dist-info}/METADATA +55 -4
- django_bulk_hooks-0.2.93.dist-info/RECORD +27 -0
- django_bulk_hooks-0.2.44.dist-info/RECORD +0 -26
- {django_bulk_hooks-0.2.44.dist-info → django_bulk_hooks-0.2.93.dist-info}/LICENSE +0 -0
- {django_bulk_hooks-0.2.44.dist-info → django_bulk_hooks-0.2.93.dist-info}/WHEEL +0 -0
django_bulk_hooks/handler.py
CHANGED
|
@@ -8,7 +8,8 @@ logger = logging.getLogger(__name__)
|
|
|
8
8
|
class HookMeta(type):
|
|
9
9
|
_registered = set()
|
|
10
10
|
_class_hook_map: dict[
|
|
11
|
-
type,
|
|
11
|
+
type,
|
|
12
|
+
set[tuple],
|
|
12
13
|
] = {} # Track which hooks belong to which class
|
|
13
14
|
|
|
14
15
|
def __new__(mcs, name, bases, namespace):
|
|
@@ -103,4 +104,3 @@ class Hook(metaclass=HookMeta):
|
|
|
103
104
|
All hook execution logic has been moved to HookDispatcher for
|
|
104
105
|
a single, consistent execution path.
|
|
105
106
|
"""
|
|
106
|
-
|
django_bulk_hooks/helpers.py
CHANGED
|
@@ -1,100 +1,258 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Helper functions for building ChangeSets from operation contexts.
|
|
3
|
-
|
|
4
|
-
These functions eliminate duplication across queryset.py, bulk_operations.py,
|
|
5
|
-
and models.py by providing reusable ChangeSet builders.
|
|
6
|
-
|
|
7
|
-
NOTE: These helpers are pure changeset builders - they don't fetch data.
|
|
8
|
-
Data fetching is the responsibility of ModelAnalyzer.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"""
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
1
|
+
"""
|
|
2
|
+
Helper functions for building ChangeSets from operation contexts.
|
|
3
|
+
|
|
4
|
+
These functions eliminate duplication across queryset.py, bulk_operations.py,
|
|
5
|
+
and models.py by providing reusable ChangeSet builders.
|
|
6
|
+
|
|
7
|
+
NOTE: These helpers are pure changeset builders - they don't fetch data.
|
|
8
|
+
Data fetching is the responsibility of ModelAnalyzer.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import logging
|
|
12
|
+
|
|
13
|
+
from django_bulk_hooks.changeset import ChangeSet
|
|
14
|
+
from django_bulk_hooks.changeset import RecordChange
|
|
15
|
+
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def extract_pks(objects):
|
|
20
|
+
"""
|
|
21
|
+
Extract non-None primary keys from objects.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
objects: Iterable of model instances or objects with pk attribute
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
List of non-None primary key values
|
|
28
|
+
"""
|
|
29
|
+
return [obj.pk for obj in objects if obj.pk is not None]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def build_changeset_for_update(
|
|
33
|
+
model_cls,
|
|
34
|
+
instances,
|
|
35
|
+
update_kwargs,
|
|
36
|
+
old_records_map=None,
|
|
37
|
+
**meta,
|
|
38
|
+
):
|
|
39
|
+
"""
|
|
40
|
+
Build ChangeSet for update operations.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
model_cls: Django model class
|
|
44
|
+
instances: List of instances being updated
|
|
45
|
+
update_kwargs: Dict of fields being updated
|
|
46
|
+
old_records_map: Optional dict of {pk: old_instance}. If None, no old records.
|
|
47
|
+
**meta: Additional metadata (e.g., has_subquery=True, lock_records=False)
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
ChangeSet instance ready for dispatcher
|
|
51
|
+
"""
|
|
52
|
+
if old_records_map is None:
|
|
53
|
+
old_records_map = {}
|
|
54
|
+
|
|
55
|
+
# Smart pre-computation logic:
|
|
56
|
+
# - If update_kwargs non-empty and old_records exist: Don't precompute (QuerySet.update case)
|
|
57
|
+
# - If update_kwargs empty and old_records exist: Don't precompute (upsert case)
|
|
58
|
+
# - If update_kwargs empty and no old_records: Precompute as empty (validation case)
|
|
59
|
+
should_precompute = not bool(update_kwargs) and old_records_map is None
|
|
60
|
+
changed_fields = list(update_kwargs.keys()) if should_precompute else None
|
|
61
|
+
|
|
62
|
+
changes = [
|
|
63
|
+
RecordChange(
|
|
64
|
+
new,
|
|
65
|
+
old_records_map.get(new.pk),
|
|
66
|
+
changed_fields=changed_fields,
|
|
67
|
+
)
|
|
68
|
+
for new in instances
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
operation_meta = {"update_kwargs": update_kwargs}
|
|
72
|
+
operation_meta.update(meta)
|
|
73
|
+
|
|
74
|
+
return ChangeSet(model_cls, changes, "update", operation_meta)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def build_changeset_for_create(model_cls, instances, **meta):
|
|
78
|
+
"""
|
|
79
|
+
Build ChangeSet for create operations.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
model_cls: Django model class
|
|
83
|
+
instances: List of instances being created
|
|
84
|
+
**meta: Additional metadata (e.g., batch_size=1000)
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
ChangeSet instance ready for dispatcher
|
|
88
|
+
"""
|
|
89
|
+
changes = [RecordChange(new, None) for new in instances]
|
|
90
|
+
return ChangeSet(model_cls, changes, "create", meta)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def build_changeset_for_delete(model_cls, instances, **meta):
|
|
94
|
+
"""
|
|
95
|
+
Build ChangeSet for delete operations.
|
|
96
|
+
|
|
97
|
+
For delete, the "new_record" is the object being deleted (current state),
|
|
98
|
+
and old_record is also the same (or None). This matches Salesforce behavior
|
|
99
|
+
where Hook.new contains the records being deleted.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
model_cls: Django model class
|
|
103
|
+
instances: List of instances being deleted
|
|
104
|
+
**meta: Additional metadata
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
ChangeSet instance ready for dispatcher
|
|
108
|
+
"""
|
|
109
|
+
changes = [
|
|
110
|
+
RecordChange(obj, obj) # new_record and old_record are the same for delete
|
|
111
|
+
for obj in instances
|
|
112
|
+
]
|
|
113
|
+
return ChangeSet(model_cls, changes, "delete", meta)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def get_fields_for_model(model_cls, field_names, include_relations=False):
|
|
117
|
+
"""
|
|
118
|
+
Get field objects for the given model from a list of field names.
|
|
119
|
+
|
|
120
|
+
Handles field name normalization (e.g., 'field_id' -> 'field').
|
|
121
|
+
Only returns fields that actually exist on the model.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
model_cls: Django model class
|
|
125
|
+
field_names: List of field names (strings)
|
|
126
|
+
include_relations: Whether to include relation fields (default False)
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
List of field objects that exist on the model, in the same order as field_names
|
|
130
|
+
"""
|
|
131
|
+
if not field_names:
|
|
132
|
+
return []
|
|
133
|
+
|
|
134
|
+
# Build lookup dict of available fields
|
|
135
|
+
fields_by_name = {}
|
|
136
|
+
# Use local_fields for child tables, get_fields() for parent tables that need inherited fields
|
|
137
|
+
fields_to_check = model_cls._meta.local_fields if not include_relations else model_cls._meta.get_fields()
|
|
138
|
+
for field in fields_to_check:
|
|
139
|
+
if not include_relations and (field.many_to_many or field.one_to_many):
|
|
140
|
+
continue
|
|
141
|
+
fields_by_name[field.name] = field
|
|
142
|
+
|
|
143
|
+
# Handle field name normalization and preserve order
|
|
144
|
+
result = []
|
|
145
|
+
seen = set()
|
|
146
|
+
|
|
147
|
+
for name in field_names:
|
|
148
|
+
# Try original name first
|
|
149
|
+
if name in fields_by_name and name not in seen:
|
|
150
|
+
result.append(fields_by_name[name])
|
|
151
|
+
seen.add(name)
|
|
152
|
+
# Try normalized name (field_id -> field)
|
|
153
|
+
elif name.endswith("_id") and name[:-3] in fields_by_name and name[:-3] not in seen:
|
|
154
|
+
result.append(fields_by_name[name[:-3]])
|
|
155
|
+
seen.add(name[:-3])
|
|
156
|
+
|
|
157
|
+
return result
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def filter_field_names_for_model(model_cls, field_names):
|
|
161
|
+
"""
|
|
162
|
+
Filter a list of field names to only those that exist on the model.
|
|
163
|
+
|
|
164
|
+
Handles field name normalization (e.g., 'field_id' -> 'field').
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
model_cls: Django model class
|
|
168
|
+
field_names: List of field names (strings)
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
List of field names that exist on the model
|
|
172
|
+
"""
|
|
173
|
+
if not field_names:
|
|
174
|
+
return []
|
|
175
|
+
|
|
176
|
+
# Get all available field names
|
|
177
|
+
available_names = {field.name for field in model_cls._meta.local_fields}
|
|
178
|
+
|
|
179
|
+
result = []
|
|
180
|
+
for name in field_names:
|
|
181
|
+
if name in available_names:
|
|
182
|
+
result.append(name)
|
|
183
|
+
elif name.endswith("_id") and name[:-3] in available_names:
|
|
184
|
+
result.append(name[:-3])
|
|
185
|
+
|
|
186
|
+
return result
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def dispatch_hooks_for_operation(changeset, event, bypass_hooks=False):
|
|
190
|
+
"""
|
|
191
|
+
Dispatch hooks for an operation using the dispatcher.
|
|
192
|
+
|
|
193
|
+
This is a convenience function that wraps the dispatcher call.
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
changeset: ChangeSet instance
|
|
197
|
+
event: Event name (e.g., 'before_update', 'after_create')
|
|
198
|
+
bypass_hooks: If True, skip hook execution
|
|
199
|
+
"""
|
|
200
|
+
from django_bulk_hooks.dispatcher import get_dispatcher
|
|
201
|
+
|
|
202
|
+
dispatcher = get_dispatcher()
|
|
203
|
+
dispatcher.dispatch(changeset, event, bypass_hooks=bypass_hooks)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def tag_upsert_metadata(result_objects, existing_record_ids, existing_pks_map):
|
|
207
|
+
"""
|
|
208
|
+
Tag objects with metadata indicating whether they were created or updated.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
result_objects: List of objects returned from bulk operation
|
|
212
|
+
existing_record_ids: Set of id() for objects that existed before
|
|
213
|
+
existing_pks_map: Dict mapping id(obj) -> pk for existing records
|
|
214
|
+
"""
|
|
215
|
+
existing_pks = set(existing_pks_map.values())
|
|
216
|
+
|
|
217
|
+
created_count = 0
|
|
218
|
+
updated_count = 0
|
|
219
|
+
|
|
220
|
+
for obj in result_objects:
|
|
221
|
+
# Use PK to determine if this record was created or updated
|
|
222
|
+
was_created = obj.pk not in existing_pks
|
|
223
|
+
obj._bulk_hooks_was_created = was_created
|
|
224
|
+
obj._bulk_hooks_upsert_metadata = True
|
|
225
|
+
|
|
226
|
+
if was_created:
|
|
227
|
+
created_count += 1
|
|
228
|
+
else:
|
|
229
|
+
updated_count += 1
|
|
230
|
+
|
|
231
|
+
logger.info(
|
|
232
|
+
f"Tagged upsert metadata: {created_count} created, {updated_count} updated "
|
|
233
|
+
f"(total={len(result_objects)}, existing_pks={len(existing_pks)})"
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def was_created(obj):
|
|
238
|
+
"""Check if an object was created in an upsert operation."""
|
|
239
|
+
return getattr(obj, "_bulk_hooks_was_created", False)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def is_upsert_result(obj):
|
|
243
|
+
"""Check if an object has upsert metadata."""
|
|
244
|
+
return getattr(obj, "_bulk_hooks_upsert_metadata", False)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def cleanup_upsert_metadata(objects):
|
|
248
|
+
"""
|
|
249
|
+
Clean up upsert metadata after hook execution.
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
objects: Objects to clean up
|
|
253
|
+
"""
|
|
254
|
+
for obj in objects:
|
|
255
|
+
if hasattr(obj, "_bulk_hooks_was_created"):
|
|
256
|
+
delattr(obj, "_bulk_hooks_was_created")
|
|
257
|
+
if hasattr(obj, "_bulk_hooks_upsert_metadata"):
|
|
258
|
+
delattr(obj, "_bulk_hooks_upsert_metadata")
|
django_bulk_hooks/manager.py
CHANGED
|
@@ -1,130 +1,134 @@
|
|
|
1
|
-
from django.db import models
|
|
2
|
-
|
|
3
|
-
from django_bulk_hooks.queryset import HookQuerySet
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"""
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
"""
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
"""
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
"""
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
1
|
+
from django.db import models
|
|
2
|
+
|
|
3
|
+
from django_bulk_hooks.queryset import HookQuerySet
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _delegate_to_queryset(self, method_name, *args, **kwargs):
|
|
7
|
+
"""
|
|
8
|
+
Generic delegation to queryset method.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
method_name: Name of the method to call on the queryset
|
|
12
|
+
*args, **kwargs: Arguments to pass to the method
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
Result of the queryset method call
|
|
16
|
+
"""
|
|
17
|
+
return getattr(self.get_queryset(), method_name)(*args, **kwargs)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class BulkHookManager(models.Manager):
|
|
21
|
+
"""
|
|
22
|
+
Manager that provides hook-aware bulk operations.
|
|
23
|
+
|
|
24
|
+
This manager automatically applies hook functionality to its querysets.
|
|
25
|
+
It can be used as a base class or composed with other managers using
|
|
26
|
+
the queryset-based approach.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def get_queryset(self):
|
|
30
|
+
"""
|
|
31
|
+
Return a HookQuerySet for this manager.
|
|
32
|
+
|
|
33
|
+
Uses the new with_hooks() method for better composition with other managers.
|
|
34
|
+
"""
|
|
35
|
+
base_queryset = super().get_queryset()
|
|
36
|
+
return HookQuerySet.with_hooks(base_queryset)
|
|
37
|
+
|
|
38
|
+
def bulk_create(
|
|
39
|
+
self,
|
|
40
|
+
objs,
|
|
41
|
+
batch_size=None,
|
|
42
|
+
ignore_conflicts=False,
|
|
43
|
+
update_conflicts=False,
|
|
44
|
+
update_fields=None,
|
|
45
|
+
unique_fields=None,
|
|
46
|
+
bypass_hooks=False,
|
|
47
|
+
**kwargs,
|
|
48
|
+
):
|
|
49
|
+
"""
|
|
50
|
+
Delegate to QuerySet's bulk_create implementation.
|
|
51
|
+
This follows Django's pattern where Manager methods call QuerySet methods.
|
|
52
|
+
"""
|
|
53
|
+
return _delegate_to_queryset(
|
|
54
|
+
self,
|
|
55
|
+
"bulk_create",
|
|
56
|
+
objs,
|
|
57
|
+
batch_size=batch_size,
|
|
58
|
+
ignore_conflicts=ignore_conflicts,
|
|
59
|
+
update_conflicts=update_conflicts,
|
|
60
|
+
update_fields=update_fields,
|
|
61
|
+
unique_fields=unique_fields,
|
|
62
|
+
bypass_hooks=bypass_hooks,
|
|
63
|
+
**kwargs,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def bulk_update(
|
|
67
|
+
self,
|
|
68
|
+
objs,
|
|
69
|
+
fields=None,
|
|
70
|
+
bypass_hooks=False,
|
|
71
|
+
**kwargs,
|
|
72
|
+
):
|
|
73
|
+
"""
|
|
74
|
+
Delegate to QuerySet's bulk_update implementation.
|
|
75
|
+
This follows Django's pattern where Manager methods call QuerySet methods.
|
|
76
|
+
|
|
77
|
+
Note: Parameters like unique_fields, update_conflicts, update_fields, and ignore_conflicts
|
|
78
|
+
are not supported by bulk_update and will be ignored with a warning.
|
|
79
|
+
These parameters are only available in bulk_create for UPSERT operations.
|
|
80
|
+
"""
|
|
81
|
+
if fields is not None:
|
|
82
|
+
kwargs["fields"] = fields
|
|
83
|
+
return _delegate_to_queryset(
|
|
84
|
+
self,
|
|
85
|
+
"bulk_update",
|
|
86
|
+
objs,
|
|
87
|
+
bypass_hooks=bypass_hooks,
|
|
88
|
+
**kwargs,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def bulk_delete(
|
|
92
|
+
self,
|
|
93
|
+
objs,
|
|
94
|
+
batch_size=None,
|
|
95
|
+
bypass_hooks=False,
|
|
96
|
+
**kwargs,
|
|
97
|
+
):
|
|
98
|
+
"""
|
|
99
|
+
Delegate to QuerySet's bulk_delete implementation.
|
|
100
|
+
This follows Django's pattern where Manager methods call QuerySet methods.
|
|
101
|
+
"""
|
|
102
|
+
return _delegate_to_queryset(
|
|
103
|
+
self,
|
|
104
|
+
"bulk_delete",
|
|
105
|
+
objs,
|
|
106
|
+
batch_size=batch_size,
|
|
107
|
+
bypass_hooks=bypass_hooks,
|
|
108
|
+
**kwargs,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
def delete(self, bypass_hooks=False):
|
|
112
|
+
"""
|
|
113
|
+
Delegate to QuerySet's delete implementation.
|
|
114
|
+
This follows Django's pattern where Manager methods call QuerySet methods.
|
|
115
|
+
"""
|
|
116
|
+
return _delegate_to_queryset(self, "delete", bypass_hooks=bypass_hooks)
|
|
117
|
+
|
|
118
|
+
def update(self, bypass_hooks=False, **kwargs):
|
|
119
|
+
"""
|
|
120
|
+
Delegate to QuerySet's update implementation.
|
|
121
|
+
This follows Django's pattern where Manager methods call QuerySet methods.
|
|
122
|
+
"""
|
|
123
|
+
return _delegate_to_queryset(self, "update", bypass_hooks=bypass_hooks, **kwargs)
|
|
124
|
+
|
|
125
|
+
def save(self, obj, bypass_hooks=False):
|
|
126
|
+
"""
|
|
127
|
+
Save a single object using the appropriate bulk operation.
|
|
128
|
+
"""
|
|
129
|
+
if obj.pk:
|
|
130
|
+
# bulk_update now auto-detects changed fields
|
|
131
|
+
self.bulk_update([obj], bypass_hooks=bypass_hooks)
|
|
132
|
+
else:
|
|
133
|
+
self.bulk_create([obj], bypass_hooks=bypass_hooks)
|
|
134
|
+
return obj
|