Skip to content

Commit

Permalink
Use django builtin for determining through field name
Browse files Browse the repository at this point in the history
Fixes #1126
  • Loading branch information
mjsir911 committed Jul 28, 2023
1 parent 5128890 commit a0f40db
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Authors
- Lucas Wiman
- Maciej "RooTer" Urbański
- Marcelo Canina (`marcanuy <https://github.com/marcanuy>`_)
- Marco Sirabella
- Mark Davidoff
- Martin Bachwerk
- Marty Alchin
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
2 changes: 1 addition & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
5 changes: 5 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
PollWithManyToManyCustomHistoryID,
PollWithManyToManyWithIPAddress,
PollWithNonEditableField,
PollWithSelfManyToMany,
PollWithSeveralManyToMany,
Province,
Restaurant,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a0f40db

Please sign in to comment.