Skip to content

Commit 2aa7f63

Browse files
committed
Fixed relation name issue brought up by Django 2.2. #3
1 parent bc44892 commit 2aa7f63

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ Compare two sets of models by `key_fields`.
8181
'updated': list of all updated objects.
8282
'updated_details': dict of {obj: {field_name: (old_value, new_value)}} for all changed fields in each updated object.
8383
'removed': list of all removed objects.
84-
} ```
84+
} ```
85+
86+
## Frameworks Supported
87+
88+
This library is tested using Python 3 against Django 1.11 and Django 2.2.

bulk_sync/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,19 @@ def compare_objs(obj1, obj2, ignore_fields=None):
125125
`key_fields`: Identifying attribute name(s) to match up `new_models` items with database rows. If a foreign key
126126
is being used as a key field, be sure to pass the `fieldname_id` rather than the `fieldname`.
127127
`ignore_fields`: (optional) If set, provide field names that should not be considered when comparing objects.
128+
If a foreign key is being used as an ignore_field, be sure to pass the `fieldname_id` rather than the `fieldname`.
128129
129130
Returns: dict of changed fields and their old/new values: {field_name: (old_value, new_value)}
130131
"""
131132

132133
ret = {}
133134
fields = obj1._meta.get_fields()
134135
for f in fields:
135-
if ignore_fields and f.name in ignore_fields:
136+
if ignore_fields and f.attname in ignore_fields:
136137
continue
137138

138-
v1 = f.to_python(getattr(obj1, f.name))
139-
v2 = f.to_python(getattr(obj2, f.name))
139+
v1 = f.to_python(getattr(obj1, f.attname))
140+
v2 = f.to_python(getattr(obj2, f.attname))
140141
if v1 != v2:
141142
ret[f.name] = (v1, v2)
142143

tests/tests.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99

1010
class BulkSyncTests(TestCase):
11+
""" Test `bulk_sync` method """
12+
1113
def setUp(self):
1214
pass
1315

@@ -75,6 +77,8 @@ def test_pk_set_but_keyfield_changes_ignores_pk(self):
7577

7678

7779
class BulkCompareTests(TestCase):
80+
""" Test `bulk_compare` method """
81+
7882
@classmethod
7983
def setUpTestData(cls):
8084
cls.c1 = Company.objects.create(name="Foo Products, Ltd.")
@@ -107,7 +111,7 @@ def test_bulk_compare(self):
107111
self.assertEqual({new_objs[0]: {"age": (40, 41)}}, ret["updated_details"])
108112
self.assertEqual([new_objs[1]], ret["unchanged"])
109113

110-
def test_bulk_compare_with_ignore_fields(self):
114+
def test_bulk_compare_with_ignore_int_field(self):
111115
c1 = self.c1
112116
e3 = self.e3
113117
new_objs = self.new_objs
@@ -124,3 +128,21 @@ def test_bulk_compare_with_ignore_fields(self):
124128
self.assertEqual([], ret["updated"])
125129
self.assertEqual({}, ret["updated_details"])
126130
self.assertEqual([new_objs[0], new_objs[1]], ret["unchanged"])
131+
132+
def test_bulk_compare_with_ignore_relation_field(self):
133+
c1 = self.c1
134+
e3 = self.e3
135+
new_objs = self.new_objs
136+
137+
ret = bulk_compare(
138+
old_models=Employee.objects.filter(company=c1).order_by("name"),
139+
new_models=new_objs,
140+
key_fields=("name",),
141+
ignore_fields=("company_id",),
142+
)
143+
144+
self.assertEqual([new_objs[2], new_objs[3]], ret["added"])
145+
self.assertEqual([e3], list(ret["removed"]))
146+
self.assertEqual([new_objs[0]], ret["updated"])
147+
self.assertEqual({new_objs[0]: {'age': (40, 41)}}, ret["updated_details"])
148+
self.assertEqual([new_objs[1]], ret["unchanged"])

0 commit comments

Comments
 (0)