diff --git a/AUTHORS.rst b/AUTHORS.rst index b9199b7e3..51c925297 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -89,6 +89,7 @@ Authors - Lucas Wiman - Maciej "RooTer" UrbaƄski - Marcelo Canina (`marcanuy `_) +- Marco Sirabella - Mark Davidoff - Martin Bachwerk - Marty Alchin diff --git a/CHANGES.rst b/CHANGES.rst index 81ced2ecc..fb4f6a6ff 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,7 @@ Unreleased ``SIMPLE_HISTORY_ENFORCE_HISTORY_MODEL_PERMISSIONS`` is set to ``True`` in ``settings`` (gh-1017). - Fixed ``SimpleHistoryAdmin`` not properly integrating with custom user models (gh-1177) +- Fixes self-m2m fields not using the correct through field name (gh-1218) 3.3.0 (2023-03-08) ------------------ diff --git a/simple_history/models.py b/simple_history/models.py index db19c66fa..906312709 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -668,7 +668,7 @@ def create_historical_record_m2ms(self, history_instance, instance): insert_rows = [] - through_field_name = type(original_instance).__name__.lower() + through_field_name = field.m2m_field_name() rows = through_model.objects.filter(**{through_field_name: instance}) diff --git a/simple_history/tests/models.py b/simple_history/tests/models.py index 5bfc74b44..fca629df1 100644 --- a/simple_history/tests/models.py +++ b/simple_history/tests/models.py @@ -200,6 +200,11 @@ class PollChildRestaurantWithManyToMany(PollParentWithManyToMany): _history_m2m_fields = [restaurants] +class PollWithSelfManyToMany(models.Model): + relations = models.ManyToManyField("self") + history = HistoricalRecords(m2m_fields=[relations]) + + class CustomAttrNameForeignKey(models.ForeignKey): def __init__(self, *args, **kwargs): self.attr_name = kwargs.pop("attr_name", None) diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py index 5dd87aeb8..53ed377da 100644 --- a/simple_history/tests/tests/test_models.py +++ b/simple_history/tests/tests/test_models.py @@ -103,6 +103,7 @@ PollWithManyToManyCustomHistoryID, PollWithManyToManyWithIPAddress, PollWithNonEditableField, + PollWithSelfManyToMany, PollWithSeveralManyToMany, Province, Restaurant, @@ -1851,6 +1852,11 @@ def test_separation(self): self.assertEqual(add.restaurants.all().count(), 0) self.assertEqual(add.places.all().count(), 0) + def test_self_field(self): + poll1 = PollWithSelfManyToMany.objects.create() + poll2 = PollWithSelfManyToMany.objects.create() + poll1.relations.add(poll2) + class ManyToManyWithSignalsTest(TestCase): def setUp(self):