Skip to content

Commit 4f4ea69

Browse files
committed
Switch to Django 2.2. bulk update.
1 parent 53e9fe7 commit 4f4ea69

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ key_fields = ('name', )
4949
ret = bulk_sync(
5050
new_models=new_models,
5151
filters=filters,
52+
fields=['name', 'phone_number', ...],
5253
key_fields=key_fields)
5354

5455
print("Results of bulk_sync: "
@@ -66,6 +67,7 @@ Combine bulk create, update, and delete. Make the DB match a set of in-memory o
6667
- `key_fields`: Identifying attribute name(s) to match up `new_models` items with database rows. If a foreign key is being used as a key field, be sure to pass the `fieldname_id` rather than the `fieldname`.
6768
- `filters`: Q() filters specifying the subset of the database to work in.
6869
- `batch_size`: passes through to Django `bulk_create.batch_size` and `bulk_update.batch_size`, and controls how many objects are created/updated per SQL query.
70+
- `fields`: a list of fields to update - passed through to Django's built in `buld_update`
6971

7072
`def bulk_compare(old_models, new_models, key_fields, ignore_fields=None):`
7173
Compare two sets of models by `key_fields`.
@@ -85,4 +87,4 @@ Compare two sets of models by `key_fields`.
8587

8688
## Frameworks Supported
8789

88-
This library is tested using Python 3 against Django 1.11 and Django 2.2.
90+
This library is tested using Python 3 against Django 2.2.

bulk_sync/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
import logging
33

44
from django.db import transaction
5-
from django_bulk_update.helper import bulk_update
65

76
logger = logging.getLogger(__name__)
87

98

10-
def bulk_sync(new_models, key_fields, filters, batch_size=None):
9+
def bulk_sync(new_models, key_fields, filters, batch_size=None, fields=None):
1110
""" Combine bulk create, update, and delete. Make the DB match a set of in-memory objects.
1211
1312
`new_models`: Django ORM objects that are the desired state. They may or may not have `id` set.
@@ -16,10 +15,17 @@ def bulk_sync(new_models, key_fields, filters, batch_size=None):
1615
`filters`: Q() filters specifying the subset of the database to work in.
1716
`batch_size`: passes through to Django `bulk_create.batch_size` and `bulk_update.batch_size`, and controls
1817
how many objects are created/updated per SQL query.
18+
`fields`: a list of fields to update - passed to django's bulk_update
1919
2020
"""
2121
db_class = new_models[0].__class__
2222

23+
if not fields:
24+
# Get a list of fields that aren't PKs and aren't editable (e.g. auto_add_now) for bulk_update
25+
fields = [field.name
26+
for field in db_class._meta.fields
27+
if not field.primary_key and not field.auto_created and field.editable]
28+
2329
with transaction.atomic():
2430
objs = db_class.objects.all()
2531
if filters:
@@ -45,8 +51,7 @@ def get_key(obj):
4551
existing_objs.append(new_obj)
4652

4753
db_class.objects.bulk_create(new_objs, batch_size=batch_size)
48-
49-
bulk_update(existing_objs, batch_size=batch_size)
54+
db_class.objects.bulk_update(existing_objs, fields=fields, batch_size=batch_size)
5055

5156
# delete stale ones...
5257
objs.filter(pk__in=[_.pk for _ in list(obj_dict.values())]).delete()

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Django>=1.8
2-
django-bulk-update>2
1+
Django>=2.2

0 commit comments

Comments
 (0)