1
1
from collections import OrderedDict
2
2
import logging
3
+
3
4
from django .db import transaction
4
5
from django .core .exceptions import FieldDoesNotExist
5
6
@@ -16,7 +17,8 @@ def bulk_sync(
16
17
`new_models`: Django ORM objects that are the desired state. They may or may not have `id` set.
17
18
`key_fields`: Identifying attribute name(s) to match up `new_models` items with database rows. If a foreign key
18
19
is being used as a key field, be sure to pass the `fieldname_id` rather than the `fieldname`.
19
- `filters`: Q() filters specifying the subset of the database to work in. Use `None` or `[]` if you want to sync against the entire table.
20
+ `filters`: Q() filters specifying the subset of the database to work in. Use `None` or `[]` if you want to sync
21
+ against the entire table.
20
22
`batch_size`: passes through to Django `bulk_create.batch_size` and `bulk_update.batch_size`, and controls
21
23
how many objects are created/updated per SQL query.
22
24
`fields`: (optional) list of fields to update. If not set, will sync all fields that are editable and not
@@ -71,7 +73,7 @@ def get_key(obj):
71
73
72
74
if not skip_creates :
73
75
db_class .objects .bulk_create (new_objs , batch_size = batch_size )
74
-
76
+
75
77
if not skip_updates :
76
78
db_class .objects .bulk_update (existing_objs , fields = fields , batch_size = batch_size )
77
79
@@ -82,8 +84,8 @@ def get_key(obj):
82
84
assert len (existing_objs ) == len (new_models ) - len (new_objs )
83
85
84
86
stats = {
85
- "created" : 0 if skip_creates else len (new_objs ),
86
- "updated" : 0 if skip_updates else (len (new_models ) - len (new_objs )),
87
+ "created" : 0 if skip_creates else len (new_objs ),
88
+ "updated" : 0 if skip_updates else (len (new_models ) - len (new_objs )),
87
89
"deleted" : 0 if skip_deletes else len (obj_dict )
88
90
}
89
91
0 commit comments