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 a history_repr field that stores the value of __str__() #1119

Closed

Conversation

alejandro-perez
Copy link

Sometimes str() requires of a foreign key to represent its value. In those scenarios, two issues appear:

  1. Need to perform extra queries. select_related() does not help since the relationship exists in the model, not the historical model.
  2. If the related instance is removed, it will fail with DoesNotExist.

One way of overcoming this situation is by storing the value of str() at the time we create the Historical Record.

Since this might have a performance penalty, it MUST be explicitly enabled by setting SIMPLE_HISTORY_HISTORIC_REPR to True.

Description

Related Issue

#533

Motivation and Context

Allowing str() that use a ForeignKey to work

How Has This Been Tested?

It hasn't, it's a pretty simple change.

Screenshots (if appropriate):

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 pre-commit run command to format and lint.
  • 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.

Sometimes __str__() requires of a foreign key to represent its value.
In those scenarios, two issues appear:

1. Need to perform extra queries. select_related() does not help since
the relationship exists in the model, not the historical model.
2. If the related instance is removed, it will fail with DoesNotExist.

One way of overcoming this situation is by storing the value of __str__()
at the time we create the Historical Record.

Since this might have a performance penalty, it MUST be explicitly
enabled by setting `SIMPLE_HISTORY_HISTORIC_REPR` to True.
@ddabble
Copy link
Member

ddabble commented Mar 15, 2024

Thank you for the PR! However, rather than adding another field, I think a better solution is to inform users of how they can customize the string representations of their historical records - potentially only using the historically saved PK of related objects, or removing the related objects from the string altogether. To do this, see my comment on #1314.

(This doesn't directly address the select_related() issue you mentioned, but for users determined to have related objects as part of their historical records' string representations, there often exist creative solutions that work around the problem :))

@ddabble
Copy link
Member

ddabble commented Mar 15, 2024

Please reopen if you'd like to discuss alternative solutions 🙂

@ddabble ddabble closed this Mar 15, 2024
@alejandro-perez
Copy link
Author

It's fine. I'm no longer using this module so I'm happy with whatever decision you take.
Thanks!

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.

2 participants