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

create_historical_record_m2ms does not respect model's excluded_field_kwargs #1358

Open
rgs258 opened this issue Jun 26, 2024 · 0 comments
Open

Comments

@rgs258
Copy link

rgs258 commented Jun 26, 2024

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:
image
...where

  1. The line where the error will occur
  2. Inspecting the contents of the row to be inserted - notice the presence of created_on
  3. The fields of the m2m_history_model - notice that created_on is missing
  4. The contents of self.excluded_field_kwargs

Error:
image
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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant