Django m2m_changed signal for GenericRelation, is it possible?

Viewed 111

Can it be used for a generic relation? I don't see it mentioned in the docs.

A naive try of getting the through table:

In [4]: Asset.related_images.through
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-d541db71a7a5> in <module>()
----> 1 Asset.related_images.through

AttributeError: 'ReverseGenericManyToOneDescriptor' object has no attribute 'through'
1 Answers

No, you can't use it. Generic relations are the backrelation of a GenericForeignKey (Many-To-One) and not a Many-To-Many-Relation. Therefore there is no through model, which links both models, but one model is directly linked via the GenericForeignKey to the other model. If you want to listen to change events you would have to connect to pre_save or post_save of the model containing the GenericForeignKey.

You could build some sort of generic Many-To-Many-Relationship with two GenericForeignKey fields in a custom through model, but Django does not support it, so you would have to send the signals on your own.

Related