Django

Code

Ticket #5390 (closed: fixed)

Opened 3 years ago

Last modified 2 months ago

Add signals to ManyRelatedManager

Reported by: Ludovico Magnocavallo <ludo@qix.it> Assigned to: rvdrijst
Milestone: 1.2 Component: Database layer (models, ORM)
Version: SVN Keywords: manytomanyfield feature signals
Cc: sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org, hv@tbz-pariv.de, django.tickets@pydev.com.ua Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

There's currently no way to run some custom code when the ManyRelatedManager? adds, removes, or clears objects. Extending the ManyRelatedManager? is very hard, since it's dynamically created each time, and the ManyRelatedObjectsDescriptor? prevents any access to it from its container class. A simple solution is to add three signals, and hook them into the dispatching mechanism when adding, removing, or deleting a related object.

A typical use case for attaching custom code to a MayRelatedManager? is blog categories: having a num_posts field in the Category models, and updating it each time a category is added/removed to/from a post, speeds up considerably a simple category list that excludes empty categories, and counts only published posts. The difference in resource usage is huge -- one query with two simple filters against one query for the list, plus one for each category (yes, you can cache the output, but think eg of a multi-blog app where posts are frequently updated, and a cache miss is very expensive), and the resulting code is much simpler.

A simple patch is attached to this ticket, I have tested it briefly against some of my models and it appears to work ok.

Attachments

related_manager.diff (2.9 kB) - added by Ludovico Magnocavallo <ludo@qix.it> on 09/10/07 11:43:53.
related_manager.2.diff (2.9 kB) - added by ludo@qix.it on 09/10/07 16:13:33.
better patch, checked against django regression tests
related.diff (2.7 kB) - added by Ludovico Magnocavallo <ludo@qix.it> on 09/11/07 10:00:08.
Cleaner variant of patch #2
models.py (3.0 kB) - added by Ludovico Magnocavallo <ludo@qix.it> on 09/11/07 10:00:32.
Test models
tests.py (1.6 kB) - added by Ludovico Magnocavallo <ludo@qix.it> on 09/11/07 10:00:52.
Test cases
m2msignals_doc.txt (1.0 kB) - added by ludo@qix.it on 01/22/08 03:53:13.
patch for the db-api docs
related.field_name.diff (4.3 kB) - added by murkt on 01/30/08 13:11:34.
related.diff with added field_name parameter (signals and dispatcher are already imported)
m2m_signals.diff (4.4 kB) - added by piranha on 08/07/08 03:40:56.
m2m signals patch, updated against signals refactoring
complete-patch.diff (16.7 kB) - added by rvdrijst on 01/23/09 11:07:41.
A complete patch with code, docs and tests for an m2m_changed signal.
5390_against_11745.diff (15.8 kB) - added by frans on 11/18/09 10:15:09.
thks Ludovico, this time with the tests
5390-against-12033.diff (18.0 kB) - added by frans on 12/31/09 05:50:18.
updated diff with correct clear() management and more recent version of trunk
t5390-r12120.1.diff (13.1 kB) - added by russellm on 01/08/10 05:52:51.
Revised implementation of m2m signals

Change History

09/10/07 11:43:53 changed by Ludovico Magnocavallo <ludo@qix.it>

  • attachment related_manager.diff added.

09/10/07 16:13:33 changed by ludo@qix.it

  • attachment related_manager.2.diff added.

better patch, checked against django regression tests

09/11/07 03:50:50 changed by Ludovico Magnocavallo <ludo@qix.it>

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Sending source_col and target_col to the dispatcher is probably unnecessary.

09/11/07 10:00:08 changed by Ludovico Magnocavallo <ludo@qix.it>

  • attachment related.diff added.

Cleaner variant of patch #2

09/11/07 10:00:32 changed by Ludovico Magnocavallo <ludo@qix.it>

  • attachment models.py added.

Test models

09/11/07 10:00:52 changed by Ludovico Magnocavallo <ludo@qix.it>

  • attachment tests.py added.

Test cases

09/11/07 10:01:52 changed by Ludovico Magnocavallo <ludo@qix.it>

I attached a cleaner patch that does away with sending the field names, and some test cases with the necessary models.

09/15/07 14:51:07 changed by PhiR

  • keywords changed from manytomanyfield to manytomanyfield feature.

09/16/07 08:25:22 changed by ubernostrum

#1069 and #2861 were duplicates.

09/16/07 13:49:14 changed by ubernostrum

#3854 was a duplicate.

10/01/07 06:25:59 changed by Silviu Julean <sjulean@ideacentrum.eu>

  • cc set to sjulean@ideacentrum.eu.

I am using this patch to monitor user-group mapping in contrib.auth, and it works without any glitches.

11/03/07 02:50:22 changed by ekarulf

  • cc changed from sjulean@ideacentrum.eu to sjulean@ideacentrum.eu, django@erik.karulf.com.

I am also using the patch to monitor contrib.auth user-group relationships with no problems.

11/03/07 03:14:16 changed by mtredinnick

  • stage changed from Unreviewed to Design decision needed.

01/18/08 12:56:32 changed by sean

I'm using the patch quite a lot and up to now it works great. I really think this functionality should go into core, simply because it complements the other signals nicely and enables a developer to fully monitor all database changes (given that the orm is used).

01/20/08 12:09:53 changed by murkt

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com.
  • needs_docs set to 1.

Patch misses documentation. Notes about m2m signals should go into db-api.txt, section Many-to-many relationships.

01/22/08 03:53:13 changed by ludo@qix.it

  • attachment m2msignals_doc.txt added.

patch for the db-api docs

01/22/08 04:00:20 changed by Ludovico Magnocavallo <ludo@qix.it>

  • needs_docs deleted.

01/24/08 17:44:49 changed by sean

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org.

As I said before the patch works, but I just found a piece of missing functionality. By using related_name it is possible to have 2 or more Many2many fields to the same model, but using the dispatcher there is no way to distinguish between the relations, or am I missing something here? A possible solution would be to send the the name of the join_table along with the signal, but that seems rather hackish.

01/30/08 12:43:51 changed by murkt

It's possible to add one parameter to RelatedManager.__init__()field_name, and send it along with the signal. With the help of field_name we can easily get field instance itself: [f for f in MyModel._meta.many_to_many if f.name == field_name][0], or access RelatedManager: getattr(instance, field_name).

01/30/08 13:11:34 changed by murkt

  • attachment related.field_name.diff added.

related.diff with added field_name parameter (signals and dispatcher are already imported)

06/14/08 07:35:08 changed by Patrick Lauber <patrick.lauber@divio.ch>

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch.

06/21/08 08:01:48 changed by anonymous

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com.

07/23/08 21:55:03 changed by anonymous

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com.

08/07/08 03:40:56 changed by piranha

  • attachment m2m_signals.diff added.

m2m signals patch, updated against signals refactoring

08/31/08 18:58:54 changed by anonymous

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com.

+1 on this, it would be so useful right now for me; too bad it isn't in yet :(

12/12/08 04:39:41 changed by rvdrijst

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst.

#6778 was a duplicate

12/19/08 11:00:04 changed by rvdrijst

  • keywords changed from manytomanyfield feature to manytomanyfield feature signals.
  • needs_better_patch set to 1.
  • needs_docs set to 1.
  • milestone set to post-1.0.

I have started a discussion on django-developers concerning this ticket and the limitations I have encountered: http://groups.google.com/group/django-developers/browse_thread/thread/afe6ad7994d868ba

Hopefully, this will push development and get this feature in the 1.1alhpa scheduled for january '09.

If you are interested, please read and comment there. If discussion is fruitful, I will claim the ticket and improve the patch, docs and tests.

Additionally, not related to the functionality but to the documentation: the db-api.txt is not the right place for signal documentation (there is no talk of pre/post_save signals there either). The correct place seems docs/ref/signals.txt. Also, not only the names of the signals, but also the extra arguments will need to be described.

12/19/08 15:09:59 changed by piranha

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua.

01/23/09 11:07:41 changed by rvdrijst

  • attachment complete-patch.diff added.

A complete patch with code, docs and tests for an m2m_changed signal.

01/23/09 11:07:57 changed by rvdrijst

  • owner changed from nobody to rvdrijst.
  • needs_better_patch deleted.
  • status changed from new to assigned.
  • needs_docs deleted.

I have attached a new patch that combines the changes to the code, the documentation and the tests in one file.

The biggest change is that there is now only one signal, m2m_changed, that has an 'action' parameter (based on a comment of Malcolm Tredinnick. The other parameters have changed somewhat, please discuss problems/suggestions on the django-developer thread mentioned above, where I have also posted a short explanation of the new parameters.

And thanks to Ludovico for the initial implementation.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

(follow-up: ↓ 24 ) 04/25/09 20:11:22 changed by wmdmark

+1 Please include this fix in the upcoming release. I'm having to do some awful code hacks to get around this.

(in reply to: ↑ 23 ; follow-ups: ↓ 31 ↓ 32 ) 04/25/09 20:15:48 changed by ubernostrum

Replying to wmdmark:

+1 Please include this fix in the upcoming release. I'm having to do some awful code hacks to get around this.

You do realize the feature freeze for 1.1 has passed already, right (in fact, 1.1 is behind schedule because we delayed a bit for a late-running feature)? No new feature checkins will happen until after the release, at which point proposals for inclusion in 1.2 will open.

If you're really interested in getting a feature into Django, it's in your best interest to get involved on the developers' list and pay attention to the release schedule.

05/16/09 19:15:52 changed by palewire

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com.

05/19/09 07:00:42 changed by schmeii

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com.

05/25/09 13:22:07 changed by UloPe

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe.

06/12/09 02:23:47 changed by lvscar

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com.

06/26/09 21:14:53 changed by bhagany

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com.

07/27/09 08:52:40 changed by msurdi

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com.

(in reply to: ↑ 24 ) 08/03/09 08:50:01 changed by schmilblick

+1 Has anyone tried the patch in production? Is it applied in the trunk?

(in reply to: ↑ 24 ) 08/18/09 04:22:11 changed by aom

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom.

Replying to ubernostrum:

If you're really interested in getting a feature into Django, it's in your best interest to get involved on the developers' list and pay attention to the release schedule.

+1 it's not a feature, it's a bug fix.

08/25/09 13:21:53 changed by natrius

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org.

08/27/09 05:05:55 changed by eromirou

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou.

09/09/09 08:47:14 changed by Tomasz Elendt

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com.

09/10/09 07:29:30 changed by twold

  • milestone set to 1.2.

Hello. What's the status of this bug? rvdrijst's patch seems to solve all the problems (at least mine, that is), it is neatly written, etc. Is there any reason for not including it into the trunk, thus finally solving this bug? I would really like to see it fixed in 1.2, because it feels really awkward patching django core on production server...

09/14/09 08:53:09 changed by jedie

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de.

09/22/09 13:09:10 changed by schmilblick

The patch solved my problems as well, but like twold wrote; it's really awkward patching stuff in prod. Status? 1.2?

11/05/09 14:39:24 changed by frans

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be.

11/08/09 12:35:51 changed by frans

I found that to have the expected behaviour when using the admin, I had to hack ReverseManyRelatedObjectsDescriptor? and ManyRelatedObjectsDescriptor? in django/db/fields/related.py to replace, in __set__,

        manager = self.__get__(instance)
        manager.clear()
        manager.add(*value)

by

        manager = self.__get__(instance)
        previous=set(manager.all())
        new=set(value)
        added=new-previous
        removed=previous-new
        manager.add(*added)
        manager.remove(*removed)

11/17/09 09:52:27 changed by schmilblick

How does the merger of Alex Gaynor's GSoC project affect this? Patch invalid, no longer needed or neither?

11/18/09 01:29:11 changed by aom

What is Alex Gaynor's GSoC project?

(It's Google Summer of Code project that aimed to bring multiple database support to Django's ORM, refactoring ManyToManyField? along the way)

11/18/09 02:32:26 changed by frans

  • needs_better_patch set to 1.

From a quick roam through the patch and the code @rev 11736 in trunk, it wouldn't take a lot to adapt. Will do if I find the time.

11/18/09 02:58:57 changed by frans

the commenter Mark says in the link posted by aom Seems like it would [solve #5390] since you'd basically be catching signals from the intermediary models. . Will test and report.

11/18/09 05:19:24 changed by russellm

@frans - No - the m2m refactor doesn't solve #5390. The signals are at a different granularity. The proposal for this ticket is to send a 'change' signal whenever the m2m field is changed; if you allow signals on the m2m intermediate, you get a signal for each and every object you assign. So -

author.books = [1,2,3,4]

Would generate 4 signals if you listened to the intermediate model, but 1 based on the proposal from this ticket.

It is for this reason that save and delete signals on auto-generated m2m models are explicitly disabled.

11/18/09 07:21:47 changed by david

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david.

I know that part of the code, I can help if you do not find the time frans.

11/18/09 10:00:04 changed by frans

added the patch. Seems to be passing tests (with only a quick try) ok. I didn't add the change mentioned in my comment above with replacing in __set__ clear and add by add and remove. It is a question of religion, I guess. Do we consider set should clear/add what you set, or add/remove the corresponding entries? I guess clear/add makes slightly less DB access. It makes sense for me to do add and remove instead of clear and add (because clear causes in one of my apps to delete some objects based on clear that I really don't want to see deleted each time the admin sets the m2m field) but if you guys have a different opinion, I'm happy with that. What shall we do?

11/18/09 10:10:40 changed by david

It looks like tests are missing from your patch (don't know if it's intentional though).

No opinion about clear/add/remove, I'm not sure I get all elements to understand.

11/18/09 10:15:09 changed by frans

  • attachment 5390_against_11745.diff added.

thks Ludovico, this time with the tests

11/24/09 10:19:36 changed by anonymous

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david to sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org.

11/24/09 10:23:46 changed by murkt

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, vsevolod.solovyov@gmail.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org to sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org.

11/30/09 01:56:38 changed by guettli

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org to sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org, hv@tbz-pariv.de.

11/30/09 05:30:48 changed by xiaket

This is really a great patch! However, when this signal is used in admin, as the setattr method is called in function save_form_data, Django would removed everything in a ManyToManyField?, and then add every object back. Thus the remove method provided in this would not be called in admin view. This is very confusing and misleading IMHO. I think someone would need to make some change to some of the functions in django/db/models/fields/related.py to make sure the remove method is called properly.

(follow-up: ↓ 54 ) 11/30/09 05:33:42 changed by frans

@xiaket : see my comment on 11/08/09 12:35:51, it precisely covers that. If you add the change I mention there, it will behave as you describe/expect.

(in reply to: ↑ 53 ) 11/30/09 05:43:47 changed by xiaket

Replying to frans:

I'm so sorry, after my stupid post I found your solution, which, I boldly suggest, should have been included in the last patch.

12/23/09 06:05:04 changed by frans

following xiaket's private email, solution in comment above should be amended to deal better with clear()

However, I think there is a minor problem about you approach: the clear signal would not be correctly issued. So I modified the code:

        manager = self.__get__(instance)
        previous=set(manager.all())
        new=set(value)
        if not new:
            manager.clear()
        else:
            added = new - previous
            removed = previous - new
            if removed:
                manager.remove(*removed)
            if added:
                manager.add(*added)

will update the patch as soon as I find time to restore my repository to a clean state...

12/24/09 08:21:46 changed by anonymous

  • cc changed from sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org, hv@tbz-pariv.de to sjulean@ideacentrum.eu, django@erik.karulf.com, sean@gmx.org, patrick.lauber@dvio.ch, chpapa@gmail.com, thiago.salves@gmail.com, oliver@bluelavatech.com, rvdrijst, piranha@piranha.org.ua, palewire@palewire.com, maxischmeii@gmail.com, django@ulo.pe, lvscar@gmail.com, brent.hagany@gmail.com, matiassurdi@gmail.com, aom, niran@niran.org, eromirou, tomasz.elendt@gmail.com, django@jensdiemer.de, francois@hop2it.be, david, chris@improbable.org, hv@tbz-pariv.de, django.tickets@pydev.com.ua.

12/31/09 05:50:18 changed by frans

  • attachment 5390-against-12033.diff added.

updated diff with correct clear() management and more recent version of trunk

12/31/09 05:55:41 changed by frans

Note : diff above (5390-against-12033.diff) is not tested yet..

01/08/10 05:52:51 changed by russellm

  • attachment t5390-r12120.1.diff added.

Revised implementation of m2m signals

01/13/10 05:07:18 changed by russellm

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [12223]) Fixed #5390 -- Added signals for m2m operations. Thanks to the many people (including, most recently, rvdrijst and frans) that have contributed to this patch.


Add/Change #5390 (Add signals to ManyRelatedManager)




Change Properties
Action