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

Exclude ManyToManyFields when fetching excluded fields. #707

Merged
merged 7 commits into from
Aug 27, 2020

Conversation

j-wing
Copy link
Contributor

@j-wing j-wing commented Aug 26, 2020

Description

Calling as_of on a model with an excluded ManyToManyField will fail if the related field has more than one object. This is because of the values(...).get() call here, which behaves differently for M2M fields than for normal scalar fields: https://github.com/jazzband/django-simple-history/blob/master/simple_history/models.py#L395.

This fixes this by simply excluding M2M fields from the reconstructed object instance. AFAICT this is safe and cheap, because users can query their M2M using the model manager if they want values from it.

An alternate fix could change the code to avoid .get(), but I don't think we actually want to prefetch all of the values of an M2M field here.

Related Issue

#706

How Has This Been Tested?

Test included.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the make format command to format my code
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

@@ -387,6 +387,7 @@ def get_instance(self):
excluded_attnames = [
model._meta.get_field(field).attname
for field in self._history_excluded_fields
if not isinstance(model._meta.get_field(field), ManyToManyField)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jordonwii this works? Looks like this excludes ManyToManyFields from being excluded? The included fields are in attrs above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's intentional - the goal here is to prevent an M2M field from ending up in the .values(...) call on line 395, because then the .get() will fail.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I see. Forgot about this code. That makes sense. Want to just add a comment here briefly explaining that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Let me know if it's unclear - wording "exclude a field from the exclusion list we're trying to re-include" is tricky. :)

@rossmechanic
Copy link
Collaborator

@jordonwii this looks good to me. Just need to fix conflicts

@j-wing
Copy link
Contributor Author

j-wing commented Aug 27, 2020

Great! The conflict was only in CHANGES.rst. It should be resolved now.

Copy link
Collaborator

@rossmechanic rossmechanic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @jordonwii !

@rossmechanic rossmechanic merged commit 2537104 into jazzband:master Aug 27, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants