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

Add excluded_field_kwargs to support custom OneToOneFields. #871

Merged
merged 2 commits into from
Oct 6, 2021

Conversation

tim-schilling
Copy link
Contributor

@tim-schilling tim-schilling commented Aug 24, 2021

Support custom OneToOneFields that specify additional arguments that don't exist in ForeignKey.__init__.

Description

These additional arguments can be specified via excluded_field_kwargs to allow these custom OneToOneFields be coerced into ForeignKeys on the historical model.

This adds the excluded_field_kwargs parameter to HistoricalRecords. It is a dictionary mapping fields to list of parameters that should be excluded when crafting the ForeignKey instance for the historical model.

Related Issue

Fixes #870

To reproduce, the following model can be added and then attempt to run makemigrations and migrate.

class CustomAttrNameOneToOneField(models.OneToOneField):
    def __init__(self, *args, **kwargs):
        self.attr_name = kwargs.pop("attr_name", None)
        super(CustomAttrNameOneToOneField, self).__init__(*args, **kwargs)

    def get_attname(self):
        return self.attr_name or super(CustomAttrNameOneToOneField, self).get_attname()

    def deconstruct(self):
        name, path, args, kwargs = super(
            CustomAttrNameOneToOneField, self
        ).deconstruct()
        if self.attr_name:
            kwargs["attr_name"] = self.attr_name
        return name, path, args, kwargs


class ModelWithCustomAttrOneToOneField(models.Model):
    poll = CustomAttrNameOneToOneField(Poll, models.CASCADE, attr_name="custom_poll")
    history = HistoricalRecords(excluded_field_kwargs={"poll": set(["attr_name"])})

Motivation and Context

Coercing OneToOneFields to be ForeignKeys on historical models causes issues when the OneToOneField is a custom subclass that includes additional arguments that do not exist on the ForeignKey.__init__ method.

How Has This Been Tested?

The problem was able to be reproduced by adding CustomAttrNameOneToOneField and ModelWithCustomAttrOneToOneField to the test app. There's an additional test that verifies the constraint of this change in that the additional arguments are not available on the ForeignKey field. This is done via TestCustomAttrOneToOneField.

Screenshots (if appropriate):

N/A

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.

@tim-schilling
Copy link
Contributor Author

I didn't check the tests case because I did not run the entire testing matrix locally.

@@ -198,13 +199,21 @@ def test_registering_with_tracked_abstract_base(self):


class TestCustomAttrForeignKey(TestCase):
""" https://github.com/jazzband/django-simple-history/issues/431 """
"""https://github.com/jazzband/django-simple-history/issues/431"""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a result of make format.

@@ -219,7 +228,7 @@ def test_migrate_command(self):


class TestModelWithHistoryInDifferentApp(TestCase):
""" https://github.com/jazzband/django-simple-history/issues/485 """
"""https://github.com/jazzband/django-simple-history/issues/485"""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a result of make format.

Coercing OneToOneFields to be ForeignKeys on historical
models causes issues when the OneToOneField is a custom
subclass that includes additional arguments that do not
exist on the ForeignKey.__init__  method.

These additional arguments can be specified via excluded_field_kwargs
to allow these custom OneToOneFields be coerced into ForeignKeys
on the historical model.

Fixes jazzband#870
@tim-schilling tim-schilling force-pushed the 807-custom-onetoonefields branch from b2e2508 to 7759125 Compare August 24, 2021 15:50
@tim-schilling
Copy link
Contributor Author

And I named the branch incorrectly... It should have been 870, not 807.

@codecov
Copy link

codecov bot commented Aug 24, 2021

Codecov Report

Merging #871 (7759125) into master (353a4ab) will decrease coverage by 0.19%.
The diff coverage is 96.96%.

❗ Current head 7759125 differs from pull request most recent head 2a5274c. Consider uploading reports for the commit 2a5274c to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #871      +/-   ##
==========================================
- Coverage   97.85%   97.65%   -0.20%     
==========================================
  Files          20       19       -1     
  Lines        1025     1023       -2     
  Branches      159      154       -5     
==========================================
- Hits         1003      999       -4     
  Misses         10       10              
- Partials       12       14       +2     
Impacted Files Coverage Δ
...istory/registry_tests/migration_test_app/models.py 94.59% <92.85%> (-1.06%) ⬇️
simple_history/models.py 97.91% <100.00%> (-0.37%) ⬇️
...ronetoonefield_modelwithcustomattronetoonefield.py 100.00% <100.00%> (ø)
simple_history/registry_tests/tests.py 96.03% <100.00%> (+0.16%) ⬆️
simple_history/manager.py 97.64% <0.00%> (-0.03%) ⬇️
simple_history/utils.py 100.00% <0.00%> (ø)
...lmodelwithcustomattrforeignkey_options_and_more.py
..._test_app/migrations/0004_history_date_indexing.py

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 353a4ab...2a5274c. Read the comment docs.

jeking3
jeking3 previously approved these changes Sep 20, 2021
Copy link
Contributor

@jeking3 jeking3 left a comment

Choose a reason for hiding this comment

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

Documentation and unit tests - looks good. Looks like there's one additional branch that did not get covered in unit tests. Need to rebase and fix up the collision in CHANGES. If this gets in before my other two I will rebase them to fix the resulting migration collisions.

@jeking3
Copy link
Contributor

jeking3 commented Sep 26, 2021

@tim-schilling there's probably just one more branch that needs coverage here, and a rebase to fix up the changes file.

@jeking3 jeking3 merged commit acf47b7 into jazzband:master Oct 6, 2021
jeking3 added a commit to cloudtruth/django-simple-history that referenced this pull request Oct 7, 2021
jeking3 added a commit that referenced this pull request Oct 13, 2021
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.

Support custom OneToOneFields with additional parameters.
2 participants