Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use django builtin for determining through field name #1218

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
mjsir911 marked this conversation as resolved.
Show resolved Hide resolved

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