-
Notifications
You must be signed in to change notification settings - Fork 489
Open
Description
Describe the bug
TypeErrors are thrown when saving a model having m2m_fields with excluded fields.
This is simply resolved by conditioning the casll to insert_row[field_name] = getattr(row, field_name)
with:
# Remove any excluded kwargs for the field.
if through_model_field.name not in self.field_excluded_kwargs(
field
):
To Reproduce
Given an m2m setup like
class Course(models.Model):
instructors = models.ManyToManyField(
settings.AUTH_USER_MODEL,
limit_choices_to=cloud_access_user_types_flat_list,
through=Instructor,
blank=True,
related_name="teaches",
)
students = models.ManyToManyField(
settings.AUTH_USER_MODEL,
limit_choices_to=cloud_access_user_types_flat_list,
through=Enrollment,
blank=True,
related_name="enrolled_in",
)
created_on = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(null=True, blank=True)
history = HistoricalRecords(
excluded_fields=[
"created_on",
"last_modified",
],
excluded_field_kwargs={
"instructors": {"created_on"},
"students": {"created_on"}
},
m2m_fields=[instructors, students]
)
Attempting to save a Course object results in this error:
File "C:\data\django\wrds\venv\Lib\site-packages\simple_history\models.py", line 639, in post_save
self.create_historical_record(instance, created and "+" or "~", using=using)
File "C:\data\django\wrds\venv\Lib\site-packages\simple_history\models.py", line 739, in create_historical_record
self.create_historical_record_m2ms(history_instance, instance)
File "C:\data\django\wrds\venv\Lib\site-packages\simple_history\models.py", line 685, in create_historical_record_m2ms
insert_rows.append(m2m_history_model(**insert_row))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\data\django\wrds\venv\Lib\site-packages\django\db\models\base.py", line 565, in __init__
raise TypeError(
TypeError: HistoricalInstructor() got unexpected keyword arguments: 'created_on'
Expected behavior
Save to proceed without error.
Screenshots
Debugging:
...where
- The line where the error will occur
- Inspecting the contents of the row to be inserted - notice the presence of
created_on
- The fields of the
m2m_history_model
- notice thatcreated_on
is missing - The contents of
self.excluded_field_kwargs
Error:
Environment (please complete the following information):
- OS: [e.g. Ubuntu 18.04]
- Browser (if applicable): [e.g. chrome, safari]
- Django Simple History Version: [e.g. 1.9.1]
- Django Version: [e.g. 1.11.11]
- Database Version: [e.g. PostgreSQL 10.5.0]
Additional context
PR incoming :)
Metadata
Metadata
Assignees
Labels
No labels