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 support for observing Many-to-Many field changes in ModelObserver #209

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

tumblingman
Copy link

Description:

This PR introduces functionality for handling m2m_changed signals within the ModelObserver class, enabling observation of changes in many-to-many relationships.

Note: This PR is dependent on #208 and must be reviewed and merged only after it. It is based on the commits from that PR.

Key Changes:

  1. Added a new signal handler (m2m_changed_receiver) to handle m2m_changed events for many-to-many fields.
  2. Updated the _connect method to dynamically register m2m_changed signals for all many-to-many fields in the observed model.
  3. Ensured appropriate messages are generated and sent for post_add, post_remove, and post_clear actions.
  4. Added warnings to highlight the limitations of many-to-many observation due to known Django issues with related object deletions and savepoint behavior.

Additional Notes:

  • The implementation includes a warning (UnsupportedWarning) to inform users about potential non-deterministic behavior when savepoints are used in transactions.
  • Comprehensive test coverage has been added to validate the new functionality.
  • Documentation for ModelObserver should be updated to reflect support for many-to-many field observation and its known limitations.

This enhancement improves the versatility of the ModelObserver, allowing for more robust real-time updates in applications with many-to-many relationships.

Ensure test consumers send JSON responses confirming subscription and unsubscription actions. Update tests to validate the new response payloads for improved accuracy and clarity.
Fix timeout error on slow machines by explicitly waiting for subscription and unsubscription confirmation before proceeding with further test execution.
Integrated support for `m2m_changed` signals in `ModelObserver` to handle many-to-many relationship changes. Updated the `database_event` method accordingly and introduced a new test to validate m2m signal handling with actions such as add, remove, and clear operations. Adjusted test data to include `groups` field for verification.
Using `str(id(self))` for `dispatch_uid` ensures compatibility with cases where signal identifiers are expected to be strings. This change prevents potential issues in environments that enforce strict type checks.
Added a warning to highlight partial support for many-to-many fields in model observation. This warns about signal issues during object deletion and potential non-deterministic behavior due to Django's use of savepoints. References a long-standing Django bug for further context.
Added checks for `through` attribute and improved handling of `pre_clear` and `reverse` cases in many-to-many field changes. Enhanced the logic to avoid duplicates and ensure correct updates to related instances. Updated tests to validate these changes, ensuring robust many-to-many relationship observation.
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.

1 participant