dispatch signal needed on many to many relationship alterations (add, delete)
|Reported by:||xore.ander@…||Owned by:||mysliceof314|
|Severity:||Keywords:||dispatcher relation m2m|
|Cc:||gsong, christian, rvdrijst||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||yes|
When a model has a relation added, no signal is sent.
This breaks functionality where the model is internally caching processed relational data, and the model becomes associated with new (or disassociated with) items. There is no method to inform the original model that it's cache is stale.
it would seem appropriate that all functions with the property 'alters_data' (originally intended for template use) would have pre/post signals.
(django/db/models/fields/related.py, django/contrib/contenttypes/generic.py: add, create, remove, clear )
to give a trivial example:
class Attendee(models.Model): name = models.CharField(maxlength=255) class Conference(models.Model): name = models.CharField(maxlength=255) attendees = models.ManyToManyField(Attendee, related_name='conferences') def num_attendees(self): if not hasattr(self,"attendee_count"): setattr(self,"attendee_count",len(self.attendees.all())) return self.attendee_count def stale_cache(self): if hasattr(self,"attendee_count"): delattr(self,"attendee_count")
(generally, i'm looking to do more complex processes than take the len() of a queryset...)
a,b,c = Attendee(name="Alice"), Attendee(name="Bob"), Conference(name="PyCon") for i in (a,b,c): i.save() c.attendees.add(a) c.num_attendees() c.attendees.add(b) c.num_attendees()
obviously calls to stale_cache() would fix the problem, but the application-level programmer shouldn't necessarily have to deal with cache maintenance. (DRY... someone's bound to forget to update cache somewhere), it would be better of there is a signal to do this on relational updates.
Enabling this should be fairly straightforward:
- add appropriate signals to django.db.models.signals
- call the dispatcher from appropriate functions in the relational model managers, etc
Change History (11)
comment:1 Changed 7 years ago by telenieko
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:4 Changed 6 years ago by mysliceof314
- Owner changed from nobody to mysliceof314
- Status changed from new to assigned
Changed 6 years ago by mysliceof314
comment:6 follow-up: ↓ 7 Changed 6 years ago by SmileyChris
- Needs documentation set
- Patch needs improvement set