ss7
ss7

Reputation: 3012

Django Models Generic Relations

I'm confused with generic relationships in Django.

I have a comment model, and I want both Workflow and WorkflowItem models to be able to have multiple comments.

If I do:

class Workflow(models.Model):
    comments = models.ManyToManyField(Comment)

class WorkflowItem(models.Model):
    comments = models.ManyToManyField(Comment)

then what do I put in the comment class to link the comment to one of these based on which it is or do I need generic relationships?

Also say I want to put members who are part of the Workflow model, do i do

class Workflow(models.Model):
    comments = models.ManyToManyField(Comment)
    members = models.ManyToManyField(Person)

or something else?

Upvotes: 0

Views: 42

Answers (1)

Rajesh Yogeshwar
Rajesh Yogeshwar

Reputation: 2179

As you mentioned that you need to link comment back to either Workflow/WorkflowItem, I believe you can structure your models as below

class Workflow(models.Model):
    members M2M field

class WorkflowItem(models.Model):
    fields

class Comment(models.Model):
    name_of_your_generic_fk(Can be either Workflow/WorkflowItem or any content type for that matter)
    fields

Using models structure like this you can trace from comment if it was made on Workflow/WorkflowItem.

You can obviously devise a better solution if you put more thought into it!! :)

Upvotes: 1

Related Questions