Opened 5 years ago

Closed 5 years ago

#31454 closed Cleanup/optimization (duplicate)

The behavior of m2m_changed signals does not match the documentation.

Reported by: Konstantin Yegupov Owned by: nobody
Component: Documentation Version: 3.0
Severity: Normal Keywords: signal, m2m_changed
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Found in v2.2.11, still appears to be valid for the master version.

The documentation at https://docs.djangoproject.com/en/3.0/ref/signals/ says:

"pre_add"
Sent before one or more objects are added to the relation.

But, in fact, pre_add is sent even if *zero* objects are added to the relation (because all the objects to be added already exist in the database).
The pk_set argument will be empty, but the signal will still fire.
The same applies to post_add, pre_remove, post_remove "actions".

Either the documentation needs to be corrected to explain that pk_set might be empty, or the code in related_descriptors.py should not fire the signals when missing_target_ids / old_ids is empty.

See also: https://code.djangoproject.com/ticket/29615

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Component: Database layer (models, ORM)Documentation
Resolution: duplicate
Status: newclosed
Summary: The behavior of m2m_changed signals does not match the documentationThe behavior of m2m_changed signals does not match the documentation.
Type: UncategorizedCleanup/optimization

It's already documented that:

pk_set

For the pre_add and post_add actions, this is a set of primary key values that will be, or have been, added to the relation. This may be a subset of the values submitted to be added, since inserts must filter existing values in order to avoid a database IntegrityError.

IMO it's sufficient, if all PKs already exist then it's empty. I don't think that an explicit note that it may be empty changes anything.

Duplicate of #27462.

Note: See TracTickets for help on using tickets.
Back to Top