Skip to content

Commit a8c08b7

Browse files
committed
Fix history_id_field not working with m2m
1 parent 4fd3ac7 commit a8c08b7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

simple_history/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def _get_history_change_reason_field(self):
400400

401401
def _get_history_id_field(self):
402402
if self.history_id_field:
403-
history_id_field = self.history_id_field
403+
history_id_field = self.history_id_field.clone()
404404
history_id_field.primary_key = True
405405
history_id_field.editable = False
406406
elif getattr(settings, "SIMPLE_HISTORY_HISTORY_ID_USE_UUID", False):

simple_history/tests/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ class PollWithManyToMany(models.Model):
117117
history = HistoricalRecords(m2m_fields=[places])
118118

119119

120+
class PollWithManyToManyCustomHistoryID(models.Model):
121+
question = models.CharField(max_length=200)
122+
pub_date = models.DateTimeField("date published")
123+
places = models.ManyToManyField("Place")
124+
125+
history = HistoricalRecords(
126+
m2m_fields=[places], history_id_field=models.UUIDField(default=uuid.uuid4)
127+
)
128+
129+
120130
class HistoricalRecordsWithExtraFieldM2M(HistoricalRecords):
121131
def get_extra_fields_m2m(self, model, through_model, fields):
122132
extra_fields = super().get_extra_fields_m2m(model, through_model, fields)

simple_history/tests/tests/test_models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
PollWithExcludeFields,
9999
PollWithHistoricalIPAddress,
100100
PollWithManyToMany,
101+
PollWithManyToManyCustomHistoryID,
101102
PollWithManyToManyWithIPAddress,
102103
PollWithNonEditableField,
103104
PollWithSeveralManyToMany,
@@ -1848,6 +1849,14 @@ def test_diff(self):
18481849
self.assertEqual(2, len(delta.changes[0].new))
18491850

18501851

1852+
class ManyToManyCustomIDTest(TestCase):
1853+
def setUp(self):
1854+
self.model = PollWithManyToManyCustomHistoryID
1855+
self.history_model = self.model.history.model
1856+
self.place = Place.objects.create(name="Home")
1857+
self.poll = self.model.objects.create(question="what's up?", pub_date=today)
1858+
1859+
18511860
class ManyToManyTest(TestCase):
18521861
def setUp(self):
18531862
self.model = PollWithManyToMany

0 commit comments

Comments
 (0)