From 01cc3f5d6d305f9f6cfe9396cb5e3662a8a21794 Mon Sep 17 00:00:00 2001 From: Ryan Sullivan Date: Wed, 26 Jun 2024 11:21:31 -0400 Subject: [PATCH] Conditions the addition to `insert_row` within `create_historical_record_m2ms` in order to respect `excluded_field_kwargs` via `self.field_excluded_kwargs`. This change necessarily undoes the addition of `through_model_field_names = [f.name for f in through_model._meta.fields]` which is not compatible with the api of `self.field_excluded_kwargs`. --- simple_history/models.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/simple_history/models.py b/simple_history/models.py index b7e137da..3d4a41b4 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -687,7 +687,6 @@ def create_historical_record_m2ms(self, history_instance, instance): m2m_history_model = self.m2m_models[field] original_instance = history_instance.instance through_model = getattr(original_instance, field.name).through - through_model_field_names = [f.name for f in through_model._meta.fields] through_model_fk_field_names = [ f.name for f in through_model._meta.fields if isinstance(f, ForeignKey) ] @@ -700,8 +699,14 @@ def create_historical_record_m2ms(self, history_instance, instance): for row in rows: insert_row = {"history": history_instance} - for field_name in through_model_field_names: - insert_row[field_name] = getattr(row, field_name) + for through_model_field in through_model._meta.fields: + # Remove any excluded kwargs for the field. + if through_model_field.name not in self.field_excluded_kwargs( + field + ): + insert_row[through_model_field.name] = getattr( + row, through_model_field.name + ) insert_rows.append(m2m_history_model(**insert_row)) pre_create_historical_m2m_records.send(