How to use a model with multiple other models

Viewed 49

This is probably pretty simple but I am having a mental block so help is appreciated. I have a discussion app which contains a Message model. I want to relate the Message to several other models (e.g. Video, Note, etc.).

I don't feel that it is right to add a field for each model onto the Message model but I want the messages to "cascade delete" when the parent object is deleted (e.g. if Video is deleted so all related Message's should be deleted as well).

How can I accomplish this?

class Message(models.Model):
    """
    Stores information pertaining to a message.
    """
    sender = models.ForeignKey(
        Employee,
        on_delete=models.CASCADE,
        null=True,
        related_name='messages'
    )
    message = models.TextField()
    parent = models.ForeignKey(
        'self',
        related_name='children',
        null=True,
        blank=True,
        on_delete=models.SET_NULL
    )
    created_on = models.DateTimeField(auto_now_add=True)

    # this seems wrong
    # video = models.FK
    # note = models.FK
    # etc...
1 Answers

Create sub classes

class models(models.Model):
    class message(models.Model):
Related