django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Reverse relation for all instances of abstract Polymorphic superclass

Open incidentist opened this issue 6 years ago • 0 comments

I have a Conversation model that is made up of many Interactions. Interaction is an abstract Polymorphic model with two concrete child models Message and VoiceCall. A conversation is all the messages and calls back and forth between two people. Interaction points back to Conversation like so:

conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, related_name='%(class)ss')

This creates properties messages and voice_calls on Conversation objects that give me all associated Interactions with those subclasses, but I also want a property interactions that gives me all Interactions associated with a Conversation, and it does not appear that Polymorphic models provide this on creation.

I understand why this is impossible with vanilla Django: I've told it that Interaction is abstract, so it should never create instances of it directly, and that's exactly what the reverse_relations would try to do. However, since Polymorphic knows how to figure out the subclass of each associated Interaction, it should be possible to get all Interactions without violating the logic of abstract Models.

Is there a way to create this reverse relation on the abstract parent class?

incidentist avatar Aug 19 '19 07:08 incidentist