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

Support custom OneToOneFields with additional parameters. #870

Closed
tim-schilling opened this issue Aug 24, 2021 · 0 comments · Fixed by #871
Closed

Support custom OneToOneFields with additional parameters. #870

tim-schilling opened this issue Aug 24, 2021 · 0 comments · Fixed by #871

Comments

@tim-schilling
Copy link
Contributor

Problem Statement
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 ForeignKey.__init__.

Describe the solution you'd like
In my particular case, ignoring the value would work well. My proposed solution is to supply a dictionary to HistoricalRecords that maps fields to the list of kwargs that should be excluded.

Describe alternatives you've considered
I did not consider alternatives.

Additional context
Currently when trying to use a custom OneToOneField, a TypeError is raised:

  File "python3.9/site-packages/simple_history/models.py", line 159, in finalize
    history_model = self.create_history_model(sender, inherited)
  File "python3.9/site-packages/simple_history/models.py", line 221, in create_history_model
    fields = self.copy_fields(model)
  File "python3.9/site-packages/simple_history/models.py", line 302, in copy_fields
    field = FieldType(*args, **field_args)
  File "python3.9/site-packages/django/db/models/fields/related.py", line 825, in __init__
    super().__init__(
  File "python3.9/site-packages/django/db/models/fields/related.py", line 474, in __init__
    super().__init__(rel=rel, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'custom_field'

This is caused by #167 and somewhat related to #462.

tim-schilling added a commit to tim-schilling/django-simple-history that referenced this issue Aug 24, 2021
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
jeking3 added a commit that referenced this issue Oct 6, 2021
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 #870

Co-authored-by: Jim King <[email protected]>
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 a pull request may close this issue.

1 participant