Skip to content

Reverse relation for all instances of abstract Polymorphic superclass #398

@incidentist

Description

@incidentist

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions