Skip to content

Commit 928354d

Browse files
committed
Add test to verify if is possible to exclude fields
1 parent 800bf33 commit 928354d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

simple_history/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,20 @@ def create_history_model(self, model):
134134
return python_2_unicode_compatible(
135135
type(str(name), self.bases, attrs))
136136

137+
def fields_included(self, model):
138+
fields = []
139+
for field in model._meta.fields:
140+
if field.name not in self.excluded_fields:
141+
fields.append(field)
142+
return fields
143+
137144
def copy_fields(self, model):
138145
"""
139146
Creates copies of the model's original fields, returning
140147
a dictionary mapping field name to copied field object.
141148
"""
142149
fields = {}
143-
fields_to_copy = set(model._meta.fields) - set(self.excluded_fields)
144-
for field in fields_to_copy:
150+
for field in self.fields_included(model):
145151
field = copy.copy(field)
146152
try:
147153
field.remote_field = copy.copy(field.remote_field)
@@ -250,7 +256,7 @@ def create_historical_record(self, instance, history_type):
250256
history_user = self.get_history_user(instance)
251257
manager = getattr(instance, self.manager_name)
252258
attrs = {}
253-
for field in instance._meta.fields:
259+
for field in self.fields_included(instance):
254260
attrs[field.attname] = getattr(instance, field.attname)
255261
manager.create(history_date=history_date, history_type=history_type,
256262
history_user=history_user, **attrs)

simple_history/tests/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class Poll(models.Model):
1818
history = HistoricalRecords()
1919

2020

21+
class PollWithExcludeFields(models.Model):
22+
question = models.CharField(max_length=200)
23+
pub_date = models.DateTimeField('date published')
24+
25+
history = HistoricalRecords(excluded_fields=['pub_date'])
26+
27+
2128
class Temperature(models.Model):
2229
location = models.CharField(max_length=200)
2330
temperature = models.IntegerField()

simple_history/tests/tests/test_models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
TrackedAbstractBaseA, TrackedAbstractBaseB,
2424
TrackedWithAbstractBase, TrackedWithConcreteBase,
2525
InheritTracking1, InheritTracking2, InheritTracking3, InheritTracking4,
26+
PollWithExcludeFields,
2627
)
2728
from ..external.models import ExternalModel2, ExternalModel4
2829

@@ -318,6 +319,14 @@ def test_foreignkey_primarykey(self):
318319
poll_info = PollInfo(poll=poll)
319320
poll_info.save()
320321

322+
def test_model_with_excluded_fields(self):
323+
p = PollWithExcludeFields(question="what's up?", pub_date=today)
324+
p.save()
325+
history = PollWithExcludeFields.history.all()[0]
326+
all_fields_names = history._meta.get_all_field_names()
327+
self.assertIn('question', all_fields_names)
328+
self.assertNotIn('pub_date', all_fields_names)
329+
321330

322331
class RegisterTest(TestCase):
323332
def test_register_no_args(self):

0 commit comments

Comments
 (0)