Skip to content

Conversation

@Jvdputten
Copy link

Changes

This PR adds the ability to include reverse relationships in the history log of a model. Previously, changes to a reverse ForeignKey (e.g., adding an Animal to a Zoo where the FK is on Animal would not appear in the Zoo's history.

Features

  • New Configuration Option: Added [include_reverse_relations] class options.

  • Reverse Relation Logging: When configured, the following actions on the child model now trigger a history entry on the parent model:

    • Creation: Creating a new child object linked to the parent.
    • Deletion: Deleting a child object linked to the parent.
    • Reassignment: Moving a child object from one parent to another (logs removal on old parent, addition on new parent).
  • Implementation: Uses pre_save and pre-delete signals on the related (child) model to detect changes.

  • Utilizing DeferredM2M type to record these relation changes in the history changeset.

  • Documentation: Updated models.md with usage examples.

  • Testing: Added comprehensive tests test_reverse_relation_history.py and test_reverse_relation_no_child_history.py covering value changes, moves, and deletions. Validated that child models do not require history enabled for this feature to work on the parent.

@Jvdputten Jvdputten force-pushed the history-reverse-relation-support branch from 9418395 to 2bd0875 Compare February 2, 2026 07:24
@Jvdputten Jvdputten force-pushed the history-reverse-relation-support branch from 2bd0875 to 41e6b89 Compare February 2, 2026 07:40
)

# This is Postgres-specific
if os.environ.get('BINDER_TEST_MYSQL', '0') != '1':

Choose a reason for hiding this comment

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

why change this?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry that was automatic formatting due to Ruff. Will revert (Highly recommend it though).

return

try:
parent = getattr(instance, fk_name)

Choose a reason for hiding this comment

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

I feel like it makes sense to make sure that, if the dev fills in a non-existant relation, that the system will raise an error to warn the dev

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