Skip to content

Commit

Permalink
Use django builtin for determining through field name (#1218)
Browse files Browse the repository at this point in the history
Fixes #1126
  • Loading branch information
mjsir911 committed Aug 19, 2023
1 parent 4b22c58 commit 78bcd0e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Unreleased
``HistoricalRecords.context.request``) under some circumstances (gh-1188)
- Made ``HistoryRequestMiddleware`` async-capable (gh-1209)
- Fixed error when setting ``table_name`` with ``inherit=True`` (gh-1195)
- Fixed ``FieldError`` when creating historical records for many-to-many fields with
``to="self"`` (gh-1218)

3.3.0 (2023-03-08)
------------------
Expand Down
3 changes: 2 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,8 @@ def create_historical_record_m2ms(self, history_instance, instance):

insert_rows = []

through_field_name = type(original_instance).__name__.lower()
# `m2m_field_name()` is part of Django's internal API
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
12 changes: 12 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 @@ -1869,6 +1870,17 @@ 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()

self.assertEqual(poll1.history.all().count(), 1)

poll1.relations.add(poll2)
self.assertIn(poll2, poll1.relations.all())

self.assertEqual(poll1.history.all().count(), 2)


class ManyToManyWithSignalsTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit 78bcd0e

Please sign in to comment.