Skip to content

Support custom OneToOneFields with additional parameters. #870

Closed
@tim-schilling

Description

@tim-schilling

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions